]> git.sesse.net Git - ffmpeg/blob - libavcodec/g723_1.h
avcodec/g723_1: Deduplicate arrays
[ffmpeg] / libavcodec / g723_1.h
1 /*
2  * G.723.1 common header and data tables
3  * Copyright (c) 2006 Benjamin Larsson
4  * Copyright (c) 2010 Mohamed Naufal Basheer
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 /**
24  * @file
25  * G.723.1 types, functions and data tables
26  */
27
28 #ifndef AVCODEC_G723_1_H
29 #define AVCODEC_G723_1_H
30
31 #include <stdint.h>
32
33 #include "libavutil/log.h"
34
35 #define SUBFRAMES       4
36 #define SUBFRAME_LEN    60
37 #define FRAME_LEN       (SUBFRAME_LEN << 2)
38 #define HALF_FRAME_LEN  (FRAME_LEN / 2)
39 #define LPC_FRAME       (HALF_FRAME_LEN + SUBFRAME_LEN)
40 #define LPC_ORDER       10
41 #define LSP_BANDS       3
42 #define LSP_CB_SIZE     256
43 #define PITCH_MIN       18
44 #define PITCH_MAX       (PITCH_MIN + 127)
45 #define PITCH_ORDER     5
46 #define GRID_SIZE       2
47 #define PULSE_MAX       6
48 #define GAIN_LEVELS     24
49 #define COS_TBL_SIZE    512
50
51 /**
52  * Bitexact implementation of 2ab scaled by 1/2^16.
53  *
54  * @param a 32 bit multiplicand
55  * @param b 16 bit multiplier
56  */
57 #define MULL2(a, b) \
58         ((((a) >> 16) * (b) * 2) + (((a) & 0xffff) * (b) >> 15))
59
60 /**
61  * G723.1 frame types
62  */
63 enum FrameType {
64     ACTIVE_FRAME,        ///< Active speech
65     SID_FRAME,           ///< Silence Insertion Descriptor frame
66     UNTRANSMITTED_FRAME
67 };
68
69 /**
70  * G723.1 rate values
71  */
72 enum Rate {
73     RATE_6300,
74     RATE_5300
75 };
76
77 /**
78  * G723.1 unpacked data subframe
79  */
80 typedef struct G723_1_Subframe {
81     int ad_cb_lag;     ///< adaptive codebook lag
82     int ad_cb_gain;
83     int dirac_train;
84     int pulse_sign;
85     int grid_index;
86     int amp_index;
87     int pulse_pos;
88 } G723_1_Subframe;
89
90 /**
91  * Pitch postfilter parameters
92  */
93 typedef struct PPFParam {
94     int     index;    ///< postfilter backward/forward lag
95     int16_t opt_gain; ///< optimal gain
96     int16_t sc_gain;  ///< scaling gain
97 } PPFParam;
98
99 /**
100  * Harmonic filter parameters
101  */
102 typedef struct HFParam {
103     int index;
104     int gain;
105 } HFParam;
106
107 /**
108  * Optimized fixed codebook excitation parameters
109  */
110 typedef struct FCBParam {
111     int min_err;
112     int amp_index;
113     int grid_index;
114     int dirac_train;
115     int pulse_pos[PULSE_MAX];
116     int pulse_sign[PULSE_MAX];
117 } FCBParam;
118
119 typedef struct G723_1_ChannelContext {
120     G723_1_Subframe subframe[4];
121     enum FrameType cur_frame_type;
122     enum FrameType past_frame_type;
123     enum Rate cur_rate;
124     uint8_t lsp_index[LSP_BANDS];
125     int pitch_lag[2];
126     int erased_frames;
127
128     int16_t prev_lsp[LPC_ORDER];
129     int16_t sid_lsp[LPC_ORDER];
130     int16_t prev_excitation[PITCH_MAX];
131     int16_t excitation[PITCH_MAX + FRAME_LEN + 4];
132     int16_t synth_mem[LPC_ORDER];
133     int16_t fir_mem[LPC_ORDER];
134     int     iir_mem[LPC_ORDER];
135
136     int random_seed;
137     int cng_random_seed;
138     int interp_index;
139     int interp_gain;
140     int sid_gain;
141     int cur_gain;
142     int reflection_coef;
143     int pf_gain;                 ///< formant postfilter
144                                  ///< gain scaling unit memory
145     int16_t audio[FRAME_LEN + LPC_ORDER + PITCH_MAX + 4];
146
147     /* encoder */
148     int16_t prev_data[HALF_FRAME_LEN];
149     int16_t prev_weight_sig[PITCH_MAX];
150
151     int16_t hpf_fir_mem;                   ///< highpass filter fir
152     int     hpf_iir_mem;                   ///< and iir memories
153     int16_t perf_fir_mem[LPC_ORDER];       ///< perceptual filter fir
154     int16_t perf_iir_mem[LPC_ORDER];       ///< and iir memories
155
156     int16_t harmonic_mem[PITCH_MAX];
157 } G723_1_ChannelContext;
158
159 typedef struct G723_1_Context {
160     AVClass *class;
161     int postfilter;
162
163     G723_1_ChannelContext ch[2];
164 } G723_1_Context;
165
166
167 /**
168  * Scale vector contents based on the largest of their absolutes.
169  */
170 int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length);
171
172 /**
173  * Calculate the number of left-shifts required for normalizing the input.
174  *
175  * @param num   input number
176  * @param width width of the input, 16 bits(0) / 32 bits(1)
177  */
178 int ff_g723_1_normalize_bits(int num, int width);
179
180 int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length);
181
182 /**
183  * Get delayed contribution from the previous excitation vector.
184  */
185 void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation,
186                             int lag);
187
188 /**
189  * Generate a train of dirac functions with period as pitch lag.
190  */
191 void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag);
192
193
194 /**
195  * Generate adaptive codebook excitation.
196  */
197 void ff_g723_1_gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
198                                   int pitch_lag, G723_1_Subframe *subfrm,
199                                   enum Rate cur_rate);
200 /**
201  * Quantize LSP frequencies by interpolation and convert them to
202  * the corresponding LPC coefficients.
203  *
204  * @param lpc      buffer for LPC coefficients
205  * @param cur_lsp  the current LSP vector
206  * @param prev_lsp the previous LSP vector
207  */
208 void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp,
209                                int16_t *prev_lsp);
210
211 /**
212  * Perform inverse quantization of LSP frequencies.
213  *
214  * @param cur_lsp    the current LSP vector
215  * @param prev_lsp   the previous LSP vector
216  * @param lsp_index  VQ indices
217  * @param bad_frame  bad frame flag
218  */
219 void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
220                              uint8_t *lsp_index, int bad_frame);
221
222 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
223
224 /**
225  * Postfilter gain weighting factors scaled by 2^15
226  */
227 static const int16_t ppf_gain_weight[2] = {0x1800, 0x2000};
228
229 /**
230  * LSP DC component
231  */
232 static const int16_t dc_lsp[LPC_ORDER] = {
233     0x0c3b,
234     0x1271,
235     0x1e0a,
236     0x2a36,
237     0x3630,
238     0x406f,
239     0x4d28,
240     0x56f4,
241     0x638c,
242     0x6c46
243 };
244
245 /* Cosine table scaled by 2^14 */
246 extern const int16_t ff_g723_1_cos_tab[COS_TBL_SIZE + 1];
247 #define G723_1_COS_TAB_FIRST_ELEMENT 16384
248
249 /**
250  *  LSP VQ tables
251  */
252 extern const int16_t ff_g723_1_lsp_band0[LSP_CB_SIZE][3];
253 extern const int16_t ff_g723_1_lsp_band1[LSP_CB_SIZE][3];
254 extern const int16_t ff_g723_1_lsp_band2[LSP_CB_SIZE][4];
255
256 /**
257  * Used for the coding/decoding of the pulses positions
258  * for the MP-MLQ codebook
259  */
260 extern const int32_t ff_g723_1_combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE];
261
262 static const int16_t pitch_contrib[340] = {
263     60,     0,  0,  2489, 60,     0,  0,  5217,
264      1,  6171,  0,  3953,  0, 10364,  1,  9357,
265     -1,  8843,  1,  9396,  0,  5794, -1, 10816,
266      2, 11606, -2, 12072,  0,  8616,  1, 12170,
267      0, 14440,  0,  7787, -1, 13721,  0, 18205,
268      0, 14471,  0, 15807,  1, 15275,  0, 13480,
269     -1, 18375, -1,     0,  1, 11194, -1, 13010,
270      1, 18836, -2, 20354,  1, 16233, -1,     0,
271     60,     0,  0, 12130,  0, 13385,  1, 17834,
272      1, 20875,  0, 21996,  1,     0,  1, 18277,
273     -1, 21321,  1, 13738, -1, 19094, -1, 20387,
274     -1,     0,  0, 21008, 60,     0, -2, 22807,
275      0, 15900,  1,     0,  0, 17989, -1, 22259,
276      1, 24395,  1, 23138,  0, 23948,  1, 22997,
277      2, 22604, -1, 25942,  0, 26246,  1, 25321,
278      0, 26423,  0, 24061,  0, 27247, 60,     0,
279     -1, 25572,  1, 23918,  1, 25930,  2, 26408,
280     -1, 19049,  1, 27357, -1, 24538, 60,     0,
281     -1, 25093,  0, 28549,  1,     0,  0, 22793,
282     -1, 25659,  0, 29377,  0, 30276,  0, 26198,
283      1, 22521, -1, 28919,  0, 27384,  1, 30162,
284     -1,     0,  0, 24237, -1, 30062,  0, 21763,
285      1, 30917, 60,     0,  0, 31284,  0, 29433,
286      1, 26821,  1, 28655,  0, 31327,  2, 30799,
287      1, 31389,  0, 32322,  1, 31760, -2, 31830,
288      0, 26936, -1, 31180,  1, 30875,  0, 27873,
289     -1, 30429,  1, 31050,  0,     0,  0, 31912,
290      1, 31611,  0, 31565,  0, 25557,  0, 31357,
291     60,     0,  1, 29536,  1, 28985, -1, 26984,
292     -1, 31587,  2, 30836, -2, 31133,  0, 30243,
293     -1, 30742, -1, 32090, 60,     0,  2, 30902,
294     60,     0,  0, 30027,  0, 29042, 60,     0,
295      0, 31756,  0, 24553,  0, 25636, -2, 30501,
296     60,     0, -1, 29617,  0, 30649, 60,     0,
297      0, 29274,  2, 30415,  0, 27480,  0, 31213,
298     -1, 28147,  0, 30600,  1, 31652,  2, 29068,
299     60,     0,  1, 28571,  1, 28730,  1, 31422,
300      0, 28257,  0, 24797, 60,     0,  0,     0,
301     60,     0,  0, 22105,  0, 27852, 60,     0,
302     60,     0, -1, 24214,  0, 24642,  0, 23305,
303     60,     0, 60,     0,  1, 22883,  0, 21601,
304     60,     0,  2, 25650, 60,     0, -2, 31253,
305     -2, 25144,  0, 17998
306 };
307
308 /**
309  * Number of non-zero pulses in the MP-MLQ excitation
310  */
311 static const int8_t pulses[4] = {6, 5, 6, 5};
312
313 /**
314  * Size of the MP-MLQ fixed excitation codebooks
315  */
316 static const int32_t max_pos[4] = {593775, 142506, 593775, 142506};
317
318 extern const int16_t ff_g723_1_fixed_cb_gain[GAIN_LEVELS];
319
320 extern const int16_t ff_g723_1_adaptive_cb_gain85 [ 85 * 20];
321 extern const int16_t ff_g723_1_adaptive_cb_gain170[170 * 20];
322
323 /**
324  * 0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15
325  */
326 static const int16_t postfilter_tbl[2][LPC_ORDER] = {
327     /* Zero */
328     {21299, 13844,  8999,  5849, 3802, 2471, 1606, 1044,  679,  441},
329     /* Pole */
330     {24576, 18432, 13824, 10368, 7776, 5832, 4374, 3281, 2460, 1845}
331 };
332
333 /**
334  * Hamming window coefficients scaled by 2^15
335  */
336 static const int16_t hamming_window[LPC_FRAME] = {
337      2621,  2631,  2659,  2705,  2770,  2853,  2955,  3074,  3212,  3367,
338      3541,  3731,  3939,  4164,  4405,  4663,  4937,  5226,  5531,  5851,
339      6186,  6534,  6897,  7273,  7661,  8062,  8475,  8899,  9334,  9780,
340     10235, 10699, 11172, 11653, 12141, 12636, 13138, 13645, 14157, 14673,
341     15193, 15716, 16242, 16769, 17298, 17827, 18356, 18884, 19411, 19935,
342     20457, 20975, 21489, 21999, 22503, 23002, 23494, 23978, 24455, 24924,
343     25384, 25834, 26274, 26704, 27122, 27529, 27924, 28306, 28675, 29031,
344     29373, 29700, 30012, 30310, 30592, 30857, 31107, 31340, 31557, 31756,
345     31938, 32102, 32249, 32377, 32488, 32580, 32654, 32710, 32747, 32766,
346     32766, 32747, 32710, 32654, 32580, 32488, 32377, 32249, 32102, 31938,
347     31756, 31557, 31340, 31107, 30857, 30592, 30310, 30012, 29700, 29373,
348     29031, 28675, 28306, 27924, 27529, 27122, 26704, 26274, 25834, 25384,
349     24924, 24455, 23978, 23494, 23002, 22503, 21999, 21489, 20975, 20457,
350     19935, 19411, 18884, 18356, 17827, 17298, 16769, 16242, 15716, 15193,
351     14673, 14157, 13645, 13138, 12636, 12141, 11653, 11172, 10699, 10235,
352      9780, 9334,   8899,  8475,  8062,  7661,  7273,  6897,  6534,  6186,
353      5851, 5531,   5226,  4937,  4663,  4405,  4164,  3939,  3731,  3541,
354      3367, 3212,   3074,  2955,  2853,  2770,  2705,  2659,  2631,  2621
355 };
356
357 /**
358  * Binomial window coefficients scaled by 2^15
359  */
360 static const int16_t binomial_window[LPC_ORDER] = {
361     32749, 32695, 32604, 32477, 32315, 32118, 31887, 31622, 31324, 30995
362 };
363
364 /**
365  * 0.994^i scaled by 2^15
366  */
367 static const int16_t bandwidth_expand[LPC_ORDER] = {
368     32571, 32376, 32182, 31989, 31797, 31606, 31416, 31228, 31040, 30854
369 };
370
371 /**
372  * 0.5^i scaled by 2^15
373  */
374 static const int16_t percept_flt_tbl[2][LPC_ORDER] = {
375     /* Zero part */
376     {29491, 26542, 23888, 21499, 19349, 17414, 15673, 14106, 12695, 11425},
377     /* Pole part */
378     {16384,  8192,  4096,  2048,  1024,   512,   256,   128,    64,    32}
379 };
380
381 static const int cng_adaptive_cb_lag[4] = { 1, 0, 1, 3 };
382
383 static const int cng_filt[4] = { 273, 998, 499, 333 };
384
385 static const int cng_bseg[3] = { 2048, 18432, 231233 };
386
387 #endif /* AVCODEC_G723_1_H */