pvamrwbdecoder_basic_op_cequivalent.h revision 4f1efc098cb5791c3e9f483f2af84aef70d2d0a0
1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18/****************************************************************************************
19Portions of this file are derived from the following 3GPP standard:
20
21    3GPP TS 26.173
22    ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23    Available from http://www.3gpp.org
24
25(C) 2007, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30------------------------------------------------------------------------------
31
32
33
34 Pathname: ./src/pvamrwbdecoder_basic_op_cequivalent.h
35
36     Date: 05/07/2007
37
38------------------------------------------------------------------------------
39 REVISION HISTORY
40
41 Description:
42------------------------------------------------------------------------------
43 INCLUDE DESCRIPTION
44
45------------------------------------------------------------------------------
46*/
47#ifndef PVAMRWBDECODER_BASIC_OP_CEQUIVALENT_H
48#define PVAMRWBDECODER_BASIC_OP_CEQUIVALENT_H
49
50#ifdef __cplusplus
51extern "C"
52{
53#endif
54
55
56#include "normalize_amr_wb.h"
57
58#if defined(C_EQUIVALENT)
59
60
61    /*----------------------------------------------------------------------------
62
63         Function Name : add_int16
64
65         Purpose :
66
67          Performs the addition (var1+var2) with overflow control and saturation;
68          the 16 bit result is set at +32767 when overflow occurs or at -32768
69          when underflow occurs.
70
71         Inputs :
72          var1
73                   16 bit short signed integer (int16) whose value falls in the
74                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
75
76          var2
77                   16 bit short signed integer (int16) whose value falls in the
78                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
79
80         Outputs :
81          none
82
83         Return Value :
84                   16 bit short signed integer (int16) whose value falls in the
85                   range : 0xffff 8000 <= var_out <= 0x0000 7fff.
86
87     ----------------------------------------------------------------------------*/
88    __inline int16 add_int16(int16 var1, int16 var2)
89    {
90        int32 L_sum;
91
92        L_sum = (int32) var1 + var2;
93        if ((L_sum >> 15) != (L_sum >> 31))
94        {
95            L_sum = (L_sum >> 31) ^ MAX_16;
96        }
97        return ((int16)(L_sum));
98    }
99
100
101    /*----------------------------------------------------------------------------
102
103         Function Name : sub_int16
104
105          Performs the subtraction (var1+var2) with overflow control and satu-
106          ration; the 16 bit result is set at +32767 when overflow occurs or at
107          -32768 when underflow occurs.
108
109         Inputs :
110
111          var1
112                   16 bit short signed integer (int16) whose value falls in the
113                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
114
115          var2
116                   16 bit short signed integer (int16) whose value falls in the
117                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
118
119         Outputs :
120          none
121
122         Return Value :
123                   16 bit short signed integer (int16) whose value falls in the
124                   range : 0xffff 8000 <= var_out <= 0x0000 7fff.
125
126     ----------------------------------------------------------------------------*/
127    __inline int16 sub_int16(int16 var1, int16 var2)
128    {
129        int32 L_diff;
130
131        L_diff = (int32) var1 - var2;
132        if ((L_diff >> 15) != (L_diff >> 31))
133        {
134            L_diff = (L_diff >> 31) ^ MAX_16;
135        }
136        return ((int16)(L_diff));
137    }
138
139
140    /*----------------------------------------------------------------------------
141
142         Function Name : mult_int16
143
144          Performs the multiplication of var1 by var2 and gives a 16 bit result
145          which is scaled i.e.:
146                   mult_int16(var1,var2) = extract_l(L_shr((var1 times var2),15)) and
147                   mult_int16(-32768,-32768) = 32767.
148
149         Inputs :
150          var1
151                   16 bit short signed integer (int16) whose value falls in the
152                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
153
154          var2
155                   16 bit short signed integer (int16) whose value falls in the
156                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
157
158
159         Return Value :
160                   16 bit short signed integer (int16) whose value falls in the
161                   range : 0xffff 8000 <= var_out <= 0x0000 7fff.
162
163     ----------------------------------------------------------------------------*/
164
165    __inline int16 mult_int16(int16 var1, int16 var2)
166    {
167        int32 L_product;
168
169        L_product = ((int32) var1 * (int32) var2) >> 15;
170
171        if ((L_product >> 15) != (L_product >> 31))
172        {
173            L_product = (L_product >> 31) ^ MAX_16;
174        }
175
176        return ((int16)L_product);
177    }
178
179
180    /*----------------------------------------------------------------------------
181
182         Function Name : add_int32
183
184         32 bits addition of the two 32 bits variables (L_var1+L_var2) with
185         overflow control and saturation; the result is set at +2147483647 when
186         overflow occurs or at -2147483648 when underflow occurs.
187
188         Inputs :
189
190          L_var1   32 bit long signed integer (int32) whose value falls in the
191                   range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
192
193          L_var2   32 bit long signed integer (int32) whose value falls in the
194                   range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
195
196
197         Return Value :
198          L_var_out
199                   32 bit long signed integer (int32) whose value falls in the
200                   range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
201
202     ----------------------------------------------------------------------------*/
203
204
205    __inline  int32 add_int32(int32 L_var1, int32 L_var2)
206    {
207        int32 L_var_out;
208
209        L_var_out = L_var1 + L_var2;
210
211        if (((L_var1 ^ L_var2) & MIN_32) == 0)  /* same sign ? */
212        {
213            if ((L_var_out ^ L_var1) & MIN_32)  /* addition matches sign ? */
214            {
215                L_var_out = (L_var1 >> 31) ^ MAX_32;
216            }
217        }
218        return (L_var_out);
219    }
220
221
222
223
224    /*----------------------------------------------------------------------------
225
226         Function Name : sub_int32
227
228         32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with
229         overflow control and saturation; the result is set at +2147483647 when
230         overflow occurs or at -2147483648 when underflow occurs.
231
232         Inputs :
233
234          L_var1   32 bit long signed integer (int32) whose value falls in the
235                   range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
236
237          L_var2   32 bit long signed integer (int32) whose value falls in the
238                   range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
239
240
241         Return Value :
242          L_var_out
243                   32 bit long signed integer (int32) whose value falls in the
244                   range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
245
246     ----------------------------------------------------------------------------*/
247
248
249    __inline  int32 sub_int32(int32 L_var1, int32 L_var2)
250    {
251        int32 L_var_out;
252
253        L_var_out = L_var1 - L_var2;
254
255        if (((L_var1 ^ L_var2) & MIN_32) != 0)  /* different sign ? */
256        {
257            if ((L_var_out ^ L_var1) & MIN_32)  /* difference matches sign ? */
258            {
259                L_var_out = (L_var1 >> 31) ^ MAX_32;
260            }
261        }
262        return (L_var_out);
263    }
264
265
266
267    /*----------------------------------------------------------------------------
268
269         Function Name : mac_16by16_to_int32
270
271         Multiply var1 by var2 and shift the result left by 1. Add the 32 bit
272         result to L_var3 with saturation, return a 32 bit result:
273              L_mac(L_var3,var1,var2) = L_add(L_var3,L_mult(var1,var2)).
274
275         Inputs :
276
277          L_var3   32 bit long signed integer (int32) whose value falls in the
278                   range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
279
280          var1
281                   16 bit short signed integer (int16) whose value falls in the
282                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
283
284          var2
285                   16 bit short signed integer (int16) whose value falls in the
286                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
287
288
289         Return Value :
290                   32 bit long signed integer (int32) whose value falls in the
291                   range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
292
293     ----------------------------------------------------------------------------*/
294
295
296    __inline  int32 mac_16by16_to_int32(int32 L_var3, int16 var1, int16 var2)
297    {
298        int32 L_var_out;
299        int32 L_mul;
300
301        L_mul  = ((int32) var1 * (int32) var2);
302
303        if (L_mul != 0x40000000)
304        {
305            L_mul <<= 1;
306        }
307        else
308        {
309            L_mul = MAX_32;     /* saturation */
310        }
311
312        L_var_out = L_var3 + L_mul;
313
314        if (((L_mul ^ L_var3) & MIN_32) == 0)  /* same sign ? */
315        {
316            if ((L_var_out ^ L_var3) & MIN_32)  /* addition matches sign ? */
317            {
318                L_var_out = (L_var3 >> 31) ^ MAX_32;
319            }
320        }
321
322        return (L_var_out);
323    }
324
325
326
327    /*----------------------------------------------------------------------------
328
329         Function Name : msu_16by16_from_int32
330
331         Multiply var1 by var2 and shift the result left by 1. Subtract the 32 bit
332         result to L_var3 with saturation, return a 32 bit result:
333              L_msu(L_var3,var1,var2) = L_sub(L_var3,L_mult(var1,var2)).
334
335         Inputs :
336
337          L_var3   32 bit long signed integer (int32) whose value falls in the
338                   range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
339
340          var1
341                   16 bit short signed integer (int16) whose value falls in the
342                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
343
344          var2
345                   16 bit short signed integer (int16) whose value falls in the
346                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
347
348
349         Return Value :
350                   32 bit long signed integer (int32) whose value falls in the
351                   range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
352
353     ----------------------------------------------------------------------------*/
354
355    __inline  int32 msu_16by16_from_int32(int32 L_var3, int16 var1, int16 var2)
356    {
357        int32 L_var_out;
358        int32 L_mul;
359
360        L_mul  = ((int32) var1 * (int32) var2);
361
362        if (L_mul != 0x40000000)
363        {
364            L_mul <<= 1;
365        }
366        else
367        {
368            L_mul = MAX_32;     /* saturation */
369        }
370
371        L_var_out = L_var3 - L_mul;
372
373        if (((L_mul ^ L_var3) & MIN_32) != 0)  /* different sign ? */
374        {
375            if ((L_var_out ^ L_var3) & MIN_32)  /* difference matches sign ? */
376            {
377                L_var_out = (L_var3 >> 31) ^ MAX_32;
378            }
379        }
380
381        return (L_var_out);
382    }
383
384
385    /*----------------------------------------------------------------------------
386
387         Function Name : mul_16by16_to_int32
388
389         mul_16by16_to_int32 is the 32 bit result of the multiplication of var1
390         times var2 with one shift left i.e.:
391              L_mult(var1,var2) = L_shl((var1 times var2),1) and
392              L_mult(-32768,-32768) = 2147483647.
393
394         Inputs :
395          var1
396                   16 bit short signed integer (int16) whose value falls in the
397                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
398
399          var2
400                   16 bit short signed integer (int16) whose value falls in the
401                   range : 0xffff 8000 <= var1 <= 0x0000 7fff.
402
403         Return Value :
404                   32 bit long signed integer (int32) whose value falls in the
405                   range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
406
407     ----------------------------------------------------------------------------*/
408
409
410    __inline  int32 mul_16by16_to_int32(int16 var1, int16 var2)
411    {
412        int32 L_mul;
413
414        L_mul  = ((int32) var1 * (int32) var2);
415
416        if (L_mul != 0x40000000)
417        {
418            L_mul <<= 1;
419        }
420        else
421        {
422            L_mul = MAX_32;     /* saturation */
423        }
424
425        return (L_mul);
426
427    }
428
429    /*----------------------------------------------------------------------------
430
431         Function Name : amr_wb_round
432
433         Round the lower 16 bits of the 32 bit input number into the MS 16 bits
434         with saturation. Shift the resulting bits right by 16 and return the 16
435         bit number:
436                     round(L_var1) = extract_h(L_add(L_var1,32768))
437
438         Inputs :
439          L_var1
440                   32 bit long signed integer (int32 ) whose value falls in the
441                   range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
442
443         Return Value :
444                   16 bit short signed integer (int16) whose value falls in the
445                   range : 0xffff 8000 <= var_out <= 0x0000 7fff.
446
447     ----------------------------------------------------------------------------*/
448    __inline int16 amr_wb_round(int32 L_var1)
449    {
450        if (L_var1 != MAX_32)
451        {
452            L_var1 +=  0x00008000L;
453        }
454        return ((int16)(L_var1 >> 16));
455    }
456
457
458    /*----------------------------------------------------------------------------
459
460         Function Name : amr_wb_shl1_round
461
462         Shift the 32 bit input number to the left by 1, round up the result and
463         shift down by 16
464                     amr_wb_shl1_round(L_var1) = round(L_shl(L_var1,1))
465
466         Inputs :
467          L_var1
468                   32 bit long signed integer (int32 ) whose value falls in the
469                   range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
470
471         Return Value :
472                   16 bit short signed integer (int16) whose value falls in the
473                   range : 0xffff 8000 <= var_out <= 0x0000 7fff.
474
475     ----------------------------------------------------------------------------*/
476    __inline int16 amr_wb_shl1_round(int32 L_var1)
477    {
478        int16 var_out;
479
480        if ((L_var1 << 1) >> 1 == L_var1)
481        {
482            var_out = (int16)((L_var1 + 0x00004000) >> 15);
483        }
484        else
485        {
486            var_out = (int16)(((L_var1 >> 31) ^ MAX_32) >> 16);
487        }
488
489        return (var_out);
490    }
491
492    /*----------------------------------------------------------------------------
493             Function Name : mul_32by16
494
495             Multiply a 16 bit integer by a 32 bit (DPF). The result is divided
496             by 2^15
497
498                    L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1
499
500             Inputs :
501
502             hi          hi part of 32 bit number.
503             lo          lo part of 32 bit number.
504             n           16 bit number.
505
506         ----------------------------------------------------------------------------*/
507
508
509    __inline int32 mul_32by16(int16 hi, int16 lo, int16 n)
510    {
511        return (((((int32)hi*n)) + ((((int32)lo*n) >> 15))) << 1);
512    }
513
514    __inline  int32 fxp_mac_16by16(int16 var1,  int16 var2, int32 L_add)
515    {
516
517        L_add += (int32)var1 * var2;
518
519        return L_add;
520    }
521
522    __inline  int32 fxp_mul_16by16(int16 var1, const int16 var2)
523    {
524        int32 L_mul = (int32)var1 * var2;
525
526        return L_mul;
527    }
528
529    __inline  int32 fxp_mul32_by_16b(int32 L_var1, const int32 L_var2)
530    {
531
532        int32 L_mul = (int32)(((int64)L_var1 * (L_var2 << 16)) >> 32);
533
534        return L_mul;
535    }
536
537
538#ifdef __cplusplus
539}
540#endif
541
542#endif
543
544#endif   /*  PVAMRWBDECODER_BASIC_OP_CEQUIVALENT_H  */
545
546