1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * adaptive and fixed codebook vector operations for ACELP-based codecs
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * Copyright (c) 2008 Vladimir Voroshilov
5cabdff1aSopenharmony_ci *
6cabdff1aSopenharmony_ci * This file is part of FFmpeg.
7cabdff1aSopenharmony_ci *
8cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
9cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
10cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
11cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
12cabdff1aSopenharmony_ci *
13cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
14cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
15cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16cabdff1aSopenharmony_ci * Lesser General Public License for more details.
17cabdff1aSopenharmony_ci *
18cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
19cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
20cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21cabdff1aSopenharmony_ci */
22cabdff1aSopenharmony_ci
23cabdff1aSopenharmony_ci#ifndef AVCODEC_ACELP_VECTORS_H
24cabdff1aSopenharmony_ci#define AVCODEC_ACELP_VECTORS_H
25cabdff1aSopenharmony_ci
26cabdff1aSopenharmony_ci#include <stdint.h>
27cabdff1aSopenharmony_ci
28cabdff1aSopenharmony_citypedef struct ACELPVContext {
29cabdff1aSopenharmony_ci    /**
30cabdff1aSopenharmony_ci     * float implementation of weighted sum of two vectors.
31cabdff1aSopenharmony_ci     * @param[out] out result of addition
32cabdff1aSopenharmony_ci     * @param in_a first vector
33cabdff1aSopenharmony_ci     * @param in_b second vector
34cabdff1aSopenharmony_ci     * @param weight_coeff_a first vector weight coefficient
35cabdff1aSopenharmony_ci     * @param weight_coeff_a second vector weight coefficient
36cabdff1aSopenharmony_ci     * @param length vectors length (should be a multiple of two)
37cabdff1aSopenharmony_ci     *
38cabdff1aSopenharmony_ci     * @note It is safe to pass the same buffer for out and in_a or in_b.
39cabdff1aSopenharmony_ci     */
40cabdff1aSopenharmony_ci    void (*weighted_vector_sumf)(float *out, const float *in_a, const float *in_b,
41cabdff1aSopenharmony_ci                                 float weight_coeff_a, float weight_coeff_b,
42cabdff1aSopenharmony_ci                                 int length);
43cabdff1aSopenharmony_ci
44cabdff1aSopenharmony_ci}ACELPVContext;
45cabdff1aSopenharmony_ci
46cabdff1aSopenharmony_ci/**
47cabdff1aSopenharmony_ci * Initialize ACELPVContext.
48cabdff1aSopenharmony_ci */
49cabdff1aSopenharmony_civoid ff_acelp_vectors_init(ACELPVContext *c);
50cabdff1aSopenharmony_civoid ff_acelp_vectors_init_mips(ACELPVContext *c);
51cabdff1aSopenharmony_ci
52cabdff1aSopenharmony_ci/** Sparse representation for the algebraic codebook (fixed) vector */
53cabdff1aSopenharmony_citypedef struct AMRFixed {
54cabdff1aSopenharmony_ci    int      n;
55cabdff1aSopenharmony_ci    int      x[10];
56cabdff1aSopenharmony_ci    float    y[10];
57cabdff1aSopenharmony_ci    int      no_repeat_mask;
58cabdff1aSopenharmony_ci    int      pitch_lag;
59cabdff1aSopenharmony_ci    float    pitch_fac;
60cabdff1aSopenharmony_ci} AMRFixed;
61cabdff1aSopenharmony_ci
62cabdff1aSopenharmony_ci/**
63cabdff1aSopenharmony_ci * Track|Pulse|        Positions
64cabdff1aSopenharmony_ci * -------------------------------------------------------------------------
65cabdff1aSopenharmony_ci *  1   | 0   | 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75
66cabdff1aSopenharmony_ci * -------------------------------------------------------------------------
67cabdff1aSopenharmony_ci *  2   | 1   | 1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76
68cabdff1aSopenharmony_ci * -------------------------------------------------------------------------
69cabdff1aSopenharmony_ci *  3   | 2   | 2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77
70cabdff1aSopenharmony_ci * -------------------------------------------------------------------------
71cabdff1aSopenharmony_ci *
72cabdff1aSopenharmony_ci * Table contains only first the pulse indexes.
73cabdff1aSopenharmony_ci *
74cabdff1aSopenharmony_ci * Used in G.729 @@8k, G.729 @@4.4k, AMR @@7.95k, AMR @@7.40k
75cabdff1aSopenharmony_ci */
76cabdff1aSopenharmony_ciextern const uint8_t ff_fc_4pulses_8bits_tracks_13[16];
77cabdff1aSopenharmony_ci
78cabdff1aSopenharmony_ci/**
79cabdff1aSopenharmony_ci * Track|Pulse|        Positions
80cabdff1aSopenharmony_ci * -------------------------------------------------------------------------
81cabdff1aSopenharmony_ci *  4   | 3   | 3, 8, 13, 18, 23, 28, 33, 38, 43, 48, 53, 58, 63, 68, 73, 78
82cabdff1aSopenharmony_ci *      |     | 4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64, 69, 74, 79
83cabdff1aSopenharmony_ci * -------------------------------------------------------------------------
84cabdff1aSopenharmony_ci *
85cabdff1aSopenharmony_ci * @remark Track in the table should be read top-to-bottom, left-to-right.
86cabdff1aSopenharmony_ci *
87cabdff1aSopenharmony_ci * Used in G.729 @@8k, G.729 @@4.4k, AMR @@7.95k, AMR @@7.40k
88cabdff1aSopenharmony_ci */
89cabdff1aSopenharmony_ciextern const uint8_t ff_fc_4pulses_8bits_track_4[32];
90cabdff1aSopenharmony_ci
91cabdff1aSopenharmony_ci/**
92cabdff1aSopenharmony_ci * Track|Pulse|        Positions
93cabdff1aSopenharmony_ci * -----------------------------------------
94cabdff1aSopenharmony_ci *  1   | 0   | 1, 6, 11, 16, 21, 26, 31, 36
95cabdff1aSopenharmony_ci *      |     | 3, 8, 13, 18, 23, 28, 33, 38
96cabdff1aSopenharmony_ci * -----------------------------------------
97cabdff1aSopenharmony_ci *
98cabdff1aSopenharmony_ci * @remark Track in the table should be read top-to-bottom, left-to-right.
99cabdff1aSopenharmony_ci *
100cabdff1aSopenharmony_ci * @note (EE) Reference G.729D code also uses gray decoding for each
101cabdff1aSopenharmony_ci *            pulse index before looking up the value in the table.
102cabdff1aSopenharmony_ci *
103cabdff1aSopenharmony_ci * Used in G.729 @@6.4k (with gray coding), AMR @@5.9k (without gray coding)
104cabdff1aSopenharmony_ci */
105cabdff1aSopenharmony_ciextern const uint8_t ff_fc_2pulses_9bits_track1_gray[16];
106cabdff1aSopenharmony_ci
107cabdff1aSopenharmony_ci/**
108cabdff1aSopenharmony_ci * Track|Pulse|        Positions
109cabdff1aSopenharmony_ci * -----------------------------------------
110cabdff1aSopenharmony_ci *  2   | 1   | 0, 7, 14, 20, 27, 34,  1, 21
111cabdff1aSopenharmony_ci *      |     | 2, 9, 15, 22, 29, 35,  6, 26
112cabdff1aSopenharmony_ci *      |     | 4,10, 17, 24, 30, 37, 11, 31
113cabdff1aSopenharmony_ci *      |     | 5,12, 19, 25, 32, 39, 16, 36
114cabdff1aSopenharmony_ci * -----------------------------------------
115cabdff1aSopenharmony_ci *
116cabdff1aSopenharmony_ci * @remark Track in the table should be read top-to-bottom, left-to-right.
117cabdff1aSopenharmony_ci *
118cabdff1aSopenharmony_ci * @note (EE.1) This table (from the reference code) does not comply with
119cabdff1aSopenharmony_ci *              the specification.
120cabdff1aSopenharmony_ci *              The specification contains the following table:
121cabdff1aSopenharmony_ci *
122cabdff1aSopenharmony_ci * Track|Pulse|        Positions
123cabdff1aSopenharmony_ci * -----------------------------------------
124cabdff1aSopenharmony_ci *  2   | 1   | 0, 5, 10, 15, 20, 25, 30, 35
125cabdff1aSopenharmony_ci *      |     | 1, 6, 11, 16, 21, 26, 31, 36
126cabdff1aSopenharmony_ci *      |     | 2, 7, 12, 17, 22, 27, 32, 37
127cabdff1aSopenharmony_ci *      |     | 4, 9, 14, 19, 24, 29, 34, 39
128cabdff1aSopenharmony_ci *
129cabdff1aSopenharmony_ci * -----------------------------------------
130cabdff1aSopenharmony_ci *
131cabdff1aSopenharmony_ci * @note (EE.2) Reference G.729D code also uses gray decoding for each
132cabdff1aSopenharmony_ci *              pulse index before looking up the value in the table.
133cabdff1aSopenharmony_ci *
134cabdff1aSopenharmony_ci * Used in G.729 @@6.4k (with gray coding)
135cabdff1aSopenharmony_ci */
136cabdff1aSopenharmony_ciextern const uint8_t ff_fc_2pulses_9bits_track2_gray[32];
137cabdff1aSopenharmony_ci
138cabdff1aSopenharmony_ci/**
139cabdff1aSopenharmony_ci * b60 hamming windowed sinc function coefficients
140cabdff1aSopenharmony_ci */
141cabdff1aSopenharmony_ciextern const float ff_b60_sinc[61];
142cabdff1aSopenharmony_ci
143cabdff1aSopenharmony_ci/**
144cabdff1aSopenharmony_ci * Table of pow(0.7,n)
145cabdff1aSopenharmony_ci */
146cabdff1aSopenharmony_ciextern const float ff_pow_0_7[10];
147cabdff1aSopenharmony_ci
148cabdff1aSopenharmony_ci/**
149cabdff1aSopenharmony_ci * Table of pow(0.75,n)
150cabdff1aSopenharmony_ci */
151cabdff1aSopenharmony_ciextern const float ff_pow_0_75[10];
152cabdff1aSopenharmony_ci
153cabdff1aSopenharmony_ci/**
154cabdff1aSopenharmony_ci * Table of pow(0.55,n)
155cabdff1aSopenharmony_ci */
156cabdff1aSopenharmony_ciextern const float ff_pow_0_55[10];
157cabdff1aSopenharmony_ci
158cabdff1aSopenharmony_ci/**
159cabdff1aSopenharmony_ci * Decode fixed-codebook vector (3.8 and D.5.8 of G.729, 5.7.1 of AMR).
160cabdff1aSopenharmony_ci * @param[out] fc_v decoded fixed codebook vector (2.13)
161cabdff1aSopenharmony_ci * @param tab1 table used for first pulse_count pulses
162cabdff1aSopenharmony_ci * @param tab2 table used for last pulse
163cabdff1aSopenharmony_ci * @param pulse_indexes fixed codebook indexes
164cabdff1aSopenharmony_ci * @param pulse_signs signs of the excitation pulses (0 bit value
165cabdff1aSopenharmony_ci *                     means negative sign)
166cabdff1aSopenharmony_ci * @param bits number of bits per one pulse index
167cabdff1aSopenharmony_ci * @param pulse_count number of pulses decoded using first table
168cabdff1aSopenharmony_ci * @param bits length of one pulse index in bits
169cabdff1aSopenharmony_ci *
170cabdff1aSopenharmony_ci * Used in G.729 @@8k, G.729 @@4.4k, G.729 @@6.4k, AMR @@7.95k, AMR @@7.40k
171cabdff1aSopenharmony_ci */
172cabdff1aSopenharmony_civoid ff_acelp_fc_pulse_per_track(int16_t* fc_v,
173cabdff1aSopenharmony_ci                                 const uint8_t *tab1,
174cabdff1aSopenharmony_ci                                 const uint8_t *tab2,
175cabdff1aSopenharmony_ci                                 int pulse_indexes,
176cabdff1aSopenharmony_ci                                 int pulse_signs,
177cabdff1aSopenharmony_ci                                 int pulse_count,
178cabdff1aSopenharmony_ci                                 int bits);
179cabdff1aSopenharmony_ci
180cabdff1aSopenharmony_ci/**
181cabdff1aSopenharmony_ci * Decode the algebraic codebook index to pulse positions and signs and
182cabdff1aSopenharmony_ci * construct the algebraic codebook vector for MODE_12k2.
183cabdff1aSopenharmony_ci *
184cabdff1aSopenharmony_ci * @note: The positions and signs are explicitly coded in MODE_12k2.
185cabdff1aSopenharmony_ci *
186cabdff1aSopenharmony_ci * @param fixed_index          positions of the ten pulses
187cabdff1aSopenharmony_ci * @param fixed_sparse         pointer to the algebraic codebook vector
188cabdff1aSopenharmony_ci * @param gray_decode          gray decoding table
189cabdff1aSopenharmony_ci * @param half_pulse_count     number of couples of pulses
190cabdff1aSopenharmony_ci * @param bits                 length of one pulse index in bits
191cabdff1aSopenharmony_ci */
192cabdff1aSopenharmony_civoid ff_decode_10_pulses_35bits(const int16_t *fixed_index,
193cabdff1aSopenharmony_ci                                AMRFixed *fixed_sparse,
194cabdff1aSopenharmony_ci                                const uint8_t *gray_decode,
195cabdff1aSopenharmony_ci                                int half_pulse_count, int bits);
196cabdff1aSopenharmony_ci
197cabdff1aSopenharmony_ci
198cabdff1aSopenharmony_ci/**
199cabdff1aSopenharmony_ci * weighted sum of two vectors with rounding.
200cabdff1aSopenharmony_ci * @param[out] out result of addition
201cabdff1aSopenharmony_ci * @param in_a first vector
202cabdff1aSopenharmony_ci * @param in_b second vector
203cabdff1aSopenharmony_ci * @param weight_coeff_a first vector weight coefficient
204cabdff1aSopenharmony_ci * @param weight_coeff_a second vector weight coefficient
205cabdff1aSopenharmony_ci * @param rounder this value will be added to the sum of the two vectors
206cabdff1aSopenharmony_ci * @param shift result will be shifted to right by this value
207cabdff1aSopenharmony_ci * @param length vectors length
208cabdff1aSopenharmony_ci *
209cabdff1aSopenharmony_ci * @note It is safe to pass the same buffer for out and in_a or in_b.
210cabdff1aSopenharmony_ci *
211cabdff1aSopenharmony_ci *  out[i] = (in_a[i]*weight_a + in_b[i]*weight_b + rounder) >> shift
212cabdff1aSopenharmony_ci */
213cabdff1aSopenharmony_civoid ff_acelp_weighted_vector_sum(int16_t* out,
214cabdff1aSopenharmony_ci                                  const int16_t *in_a,
215cabdff1aSopenharmony_ci                                  const int16_t *in_b,
216cabdff1aSopenharmony_ci                                  int16_t weight_coeff_a,
217cabdff1aSopenharmony_ci                                  int16_t weight_coeff_b,
218cabdff1aSopenharmony_ci                                  int16_t rounder,
219cabdff1aSopenharmony_ci                                  int shift,
220cabdff1aSopenharmony_ci                                  int length);
221cabdff1aSopenharmony_ci
222cabdff1aSopenharmony_ci/**
223cabdff1aSopenharmony_ci * float implementation of weighted sum of two vectors.
224cabdff1aSopenharmony_ci * @param[out] out result of addition
225cabdff1aSopenharmony_ci * @param in_a first vector
226cabdff1aSopenharmony_ci * @param in_b second vector
227cabdff1aSopenharmony_ci * @param weight_coeff_a first vector weight coefficient
228cabdff1aSopenharmony_ci * @param weight_coeff_a second vector weight coefficient
229cabdff1aSopenharmony_ci * @param length vectors length
230cabdff1aSopenharmony_ci *
231cabdff1aSopenharmony_ci * @note It is safe to pass the same buffer for out and in_a or in_b.
232cabdff1aSopenharmony_ci */
233cabdff1aSopenharmony_civoid ff_weighted_vector_sumf(float *out, const float *in_a, const float *in_b,
234cabdff1aSopenharmony_ci                             float weight_coeff_a, float weight_coeff_b,
235cabdff1aSopenharmony_ci                             int length);
236cabdff1aSopenharmony_ci
237cabdff1aSopenharmony_ci/**
238cabdff1aSopenharmony_ci * Adaptive gain control (as used in AMR postfiltering)
239cabdff1aSopenharmony_ci *
240cabdff1aSopenharmony_ci * @param out output buffer for filtered speech data
241cabdff1aSopenharmony_ci * @param in the input speech buffer (may be the same as out)
242cabdff1aSopenharmony_ci * @param speech_energ input energy
243cabdff1aSopenharmony_ci * @param size the input buffer size
244cabdff1aSopenharmony_ci * @param alpha exponential filter factor
245cabdff1aSopenharmony_ci * @param gain_mem a pointer to the filter memory (single float of size)
246cabdff1aSopenharmony_ci */
247cabdff1aSopenharmony_civoid ff_adaptive_gain_control(float *out, const float *in, float speech_energ,
248cabdff1aSopenharmony_ci                              int size, float alpha, float *gain_mem);
249cabdff1aSopenharmony_ci
250cabdff1aSopenharmony_ci/**
251cabdff1aSopenharmony_ci * Set the sum of squares of a signal by scaling
252cabdff1aSopenharmony_ci *
253cabdff1aSopenharmony_ci * @param out output samples
254cabdff1aSopenharmony_ci * @param in input samples
255cabdff1aSopenharmony_ci * @param sum_of_squares new sum of squares
256cabdff1aSopenharmony_ci * @param n number of samples
257cabdff1aSopenharmony_ci *
258cabdff1aSopenharmony_ci * @note If the input is zero (or its energy underflows), the output is zero.
259cabdff1aSopenharmony_ci *       This is the behavior of AGC in the AMR reference decoder. The QCELP
260cabdff1aSopenharmony_ci *       reference decoder seems to have undefined behavior.
261cabdff1aSopenharmony_ci *
262cabdff1aSopenharmony_ci * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6
263cabdff1aSopenharmony_ci * 3GPP TS 26.090 6.1 (6)
264cabdff1aSopenharmony_ci */
265cabdff1aSopenharmony_civoid ff_scale_vector_to_given_sum_of_squares(float *out, const float *in,
266cabdff1aSopenharmony_ci                                             float sum_of_squares, const int n);
267cabdff1aSopenharmony_ci
268cabdff1aSopenharmony_ci/**
269cabdff1aSopenharmony_ci * Add fixed vector to an array from a sparse representation
270cabdff1aSopenharmony_ci *
271cabdff1aSopenharmony_ci * @param out fixed vector with pitch sharpening
272cabdff1aSopenharmony_ci * @param in sparse fixed vector
273cabdff1aSopenharmony_ci * @param scale number to multiply the fixed vector by
274cabdff1aSopenharmony_ci * @param size the output vector size
275cabdff1aSopenharmony_ci */
276cabdff1aSopenharmony_civoid ff_set_fixed_vector(float *out, const AMRFixed *in, float scale, int size);
277cabdff1aSopenharmony_ci
278cabdff1aSopenharmony_ci/**
279cabdff1aSopenharmony_ci * Clear array values set by set_fixed_vector
280cabdff1aSopenharmony_ci *
281cabdff1aSopenharmony_ci * @param out fixed vector to be cleared
282cabdff1aSopenharmony_ci * @param in sparse fixed vector
283cabdff1aSopenharmony_ci * @param size the output vector size
284cabdff1aSopenharmony_ci */
285cabdff1aSopenharmony_civoid ff_clear_fixed_vector(float *out, const AMRFixed *in, int size);
286cabdff1aSopenharmony_ci
287cabdff1aSopenharmony_ci#endif /* AVCODEC_ACELP_VECTORS_H */
288