2 * G.723.1 common header and data tables
3 * Copyright (c) 2006 Benjamin Larsson
4 * Copyright (c) 2010 Mohamed Naufal Basheer
6 * This file is part of FFmpeg.
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.
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.
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
25 * G.723.1 types, functions and data tables
28 #ifndef AVCODEC_G723_1_H
29 #define AVCODEC_G723_1_H
33 #include "libavutil/log.h"
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)
42 #define LSP_CB_SIZE 256
44 #define PITCH_MAX (PITCH_MIN + 127)
48 #define GAIN_LEVELS 24
49 #define COS_TBL_SIZE 512
52 * Bitexact implementation of 2ab scaled by 1/2^16.
54 * @param a 32 bit multiplicand
55 * @param b 16 bit multiplier
58 ((((a) >> 16) * (b) * 2) + (((a) & 0xffff) * (b) >> 15))
64 ACTIVE_FRAME, ///< Active speech
65 SID_FRAME, ///< Silence Insertion Descriptor frame
78 * G723.1 unpacked data subframe
80 typedef struct G723_1_Subframe {
81 int ad_cb_lag; ///< adaptive codebook lag
91 * Pitch postfilter parameters
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
100 * Harmonic filter parameters
102 typedef struct HFParam {
108 * Optimized fixed codebook excitation parameters
110 typedef struct FCBParam {
115 int pulse_pos[PULSE_MAX];
116 int pulse_sign[PULSE_MAX];
119 typedef struct G723_1_ChannelContext {
120 G723_1_Subframe subframe[4];
121 enum FrameType cur_frame_type;
122 enum FrameType past_frame_type;
124 uint8_t lsp_index[LSP_BANDS];
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];
143 int pf_gain; ///< formant postfilter
144 ///< gain scaling unit memory
145 int16_t audio[FRAME_LEN + LPC_ORDER + PITCH_MAX + 4];
148 int16_t prev_data[HALF_FRAME_LEN];
149 int16_t prev_weight_sig[PITCH_MAX];
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
156 int16_t harmonic_mem[PITCH_MAX];
157 } G723_1_ChannelContext;
159 typedef struct G723_1_Context {
163 G723_1_ChannelContext ch[2];
168 * Scale vector contents based on the largest of their absolutes.
170 int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length);
173 * Calculate the number of left-shifts required for normalizing the input.
175 * @param num input number
176 * @param width width of the input, 16 bits(0) / 32 bits(1)
178 int ff_g723_1_normalize_bits(int num, int width);
180 int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length);
183 * Get delayed contribution from the previous excitation vector.
185 void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation,
189 * Generate a train of dirac functions with period as pitch lag.
191 void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag);
195 * Generate adaptive codebook excitation.
197 void ff_g723_1_gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
198 int pitch_lag, G723_1_Subframe *subfrm,
201 * Quantize LSP frequencies by interpolation and convert them to
202 * the corresponding LPC coefficients.
204 * @param lpc buffer for LPC coefficients
205 * @param cur_lsp the current LSP vector
206 * @param prev_lsp the previous LSP vector
208 void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp,
212 * Perform inverse quantization of LSP frequencies.
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
219 void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
220 uint8_t *lsp_index, int bad_frame);
222 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
225 * Postfilter gain weighting factors scaled by 2^15
227 static const int16_t ppf_gain_weight[2] = {0x1800, 0x2000};
232 static const int16_t dc_lsp[LPC_ORDER] = {
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
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];
257 * Used for the coding/decoding of the pulses positions
258 * for the MP-MLQ codebook
260 extern const int32_t ff_g723_1_combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE];
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,
309 * Number of non-zero pulses in the MP-MLQ excitation
311 static const int8_t pulses[4] = {6, 5, 6, 5};
314 * Size of the MP-MLQ fixed excitation codebooks
316 static const int32_t max_pos[4] = {593775, 142506, 593775, 142506};
318 extern const int16_t ff_g723_1_fixed_cb_gain[GAIN_LEVELS];
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];
324 * 0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15
326 static const int16_t postfilter_tbl[2][LPC_ORDER] = {
328 {21299, 13844, 8999, 5849, 3802, 2471, 1606, 1044, 679, 441},
330 {24576, 18432, 13824, 10368, 7776, 5832, 4374, 3281, 2460, 1845}
334 * Hamming window coefficients scaled by 2^15
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
358 * Binomial window coefficients scaled by 2^15
360 static const int16_t binomial_window[LPC_ORDER] = {
361 32749, 32695, 32604, 32477, 32315, 32118, 31887, 31622, 31324, 30995
365 * 0.994^i scaled by 2^15
367 static const int16_t bandwidth_expand[LPC_ORDER] = {
368 32571, 32376, 32182, 31989, 31797, 31606, 31416, 31228, 31040, 30854
372 * 0.5^i scaled by 2^15
374 static const int16_t percept_flt_tbl[2][LPC_ORDER] = {
376 {29491, 26542, 23888, 21499, 19349, 17414, 15673, 14106, 12695, 11425},
378 {16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32}
381 static const int cng_adaptive_cb_lag[4] = { 1, 0, 1, 3 };
383 static const int cng_filt[4] = { 273, 998, 499, 333 };
385 static const int cng_bseg[3] = { 2048, 18432, 231233 };
387 #endif /* AVCODEC_G723_1_H */