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 };
227 static const int16_t dc_lsp[LPC_ORDER] = {
240 /* Cosine table scaled by 2^14 */
241 extern const int16_t ff_g723_1_cos_tab[COS_TBL_SIZE + 1];
242 #define G723_1_COS_TAB_FIRST_ELEMENT 16384
247 extern const int16_t ff_g723_1_lsp_band0[LSP_CB_SIZE][3];
248 extern const int16_t ff_g723_1_lsp_band1[LSP_CB_SIZE][3];
249 extern const int16_t ff_g723_1_lsp_band2[LSP_CB_SIZE][4];
252 * Used for the coding/decoding of the pulses positions
253 * for the MP-MLQ codebook
255 extern const int32_t ff_g723_1_combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE];
258 * Number of non-zero pulses in the MP-MLQ excitation
260 static const int8_t pulses[4] = {6, 5, 6, 5};
262 extern const int16_t ff_g723_1_fixed_cb_gain[GAIN_LEVELS];
264 extern const int16_t ff_g723_1_adaptive_cb_gain85 [ 85 * 20];
265 extern const int16_t ff_g723_1_adaptive_cb_gain170[170 * 20];
267 #endif /* AVCODEC_G723_1_H */