1/*
2 * adaptive and fixed codebook vector operations for ACELP-based codecs
3 *
4 * Copyright (c) 2008 Vladimir Voroshilov
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <inttypes.h>
24#include "avcodec.h"
25#include "acelp_vectors.h"
26
27const uint8_t ff_fc_2pulses_9bits_track1[16] =
28{
29    1,  3,
30    6,  8,
31    11, 13,
32    16, 18,
33    21, 23,
34    26, 28,
35    31, 33,
36    36, 38
37};
38const uint8_t ff_fc_2pulses_9bits_track1_gray[16] =
39{
40  1,  3,
41  8,  6,
42  18, 16,
43  11, 13,
44  38, 36,
45  31, 33,
46  21, 23,
47  28, 26,
48};
49
50const uint8_t ff_fc_2pulses_9bits_track2_gray[32] =
51{
52  0,  2,
53  5,  4,
54  12, 10,
55  7,  9,
56  25, 24,
57  20, 22,
58  14, 15,
59  19, 17,
60  36, 31,
61  21, 26,
62  1,  6,
63  16, 11,
64  27, 29,
65  32, 30,
66  39, 37,
67  34, 35,
68};
69
70const uint8_t ff_fc_4pulses_8bits_tracks_13[16] =
71{
72  0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75,
73};
74
75const uint8_t ff_fc_4pulses_8bits_track_4[32] =
76{
77    3,  4,
78    8,  9,
79    13, 14,
80    18, 19,
81    23, 24,
82    28, 29,
83    33, 34,
84    38, 39,
85    43, 44,
86    48, 49,
87    53, 54,
88    58, 59,
89    63, 64,
90    68, 69,
91    73, 74,
92    78, 79,
93};
94
95#if 0
96static uint8_t gray_decode[32] =
97{
98    0,  1,  3,  2,  7,  6,  4,  5,
99   15, 14, 12, 13,  8,  9, 11, 10,
100   31, 30, 28, 29, 24, 25, 27, 26,
101   16, 17, 19, 18, 23, 22, 20, 21
102};
103#endif
104
105void ff_acelp_fc_pulse_per_track(
106        int16_t* fc_v,
107        const uint8_t *tab1,
108        const uint8_t *tab2,
109        int pulse_indexes,
110        int pulse_signs,
111        int pulse_count,
112        int bits)
113{
114    int mask = (1 << bits) - 1;
115    int i;
116
117    for(i=0; i<pulse_count; i++)
118    {
119        fc_v[i + tab1[pulse_indexes & mask]] +=
120                (pulse_signs & 1) ? 8191 : -8192; // +/-1 in (2.13)
121
122        pulse_indexes >>= bits;
123        pulse_signs >>= 1;
124    }
125
126    fc_v[tab2[pulse_indexes]] += (pulse_signs & 1) ? 8191 : -8192;
127}
128
129void ff_acelp_weighted_vector_sum(
130        int16_t* out,
131        const int16_t *in_a,
132        const int16_t *in_b,
133        int16_t weight_coeff_a,
134        int16_t weight_coeff_b,
135        int16_t rounder,
136        int shift,
137        int length)
138{
139    int i;
140
141    // Clipping required here; breaks OVERFLOW test.
142    for(i=0; i<length; i++)
143        out[i] = av_clip_int16((
144                 in_a[i] * weight_coeff_a +
145                 in_b[i] * weight_coeff_b +
146                 rounder) >> shift);
147}
148