• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * LSP DC component
226  */
227 static const int16_t dc_lsp[LPC_ORDER] = {
228     0x0c3b,
229     0x1271,
230     0x1e0a,
231     0x2a36,
232     0x3630,
233     0x406f,
234     0x4d28,
235     0x56f4,
236     0x638c,
237     0x6c46
238 };
239 
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
243 
244 /**
245  *  LSP VQ tables
246  */
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];
250 
251 /**
252  * Used for the coding/decoding of the pulses positions
253  * for the MP-MLQ codebook
254  */
255 extern const int32_t ff_g723_1_combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE];
256 
257 /**
258  * Number of non-zero pulses in the MP-MLQ excitation
259  */
260 static const int8_t pulses[4] = {6, 5, 6, 5};
261 
262 extern const int16_t ff_g723_1_fixed_cb_gain[GAIN_LEVELS];
263 
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];
266 
267 #endif /* AVCODEC_G723_1_H */
268