FFmpeg
g723_1.h
Go to the documentation of this file.
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
67 };
68 
69 /**
70  * G723.1 rate values
71  */
72 enum Rate {
75 };
76 
77 /**
78  * G723.1 unpacked data subframe
79  */
80 typedef struct G723_1_Subframe {
81  int ad_cb_lag; ///< adaptive codebook lag
86  int amp_index;
87  int pulse_pos;
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;
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];
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 
140  int sid_gain;
141  int cur_gain;
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];
158 
159 typedef struct G723_1_Context {
160  AVClass *class;
162 
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  */
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 */
const int16_t ff_g723_1_adaptive_cb_gain170[170 *20]
Definition: g723_1.c:675
#define COS_TBL_SIZE
Definition: g723_1.h:49
int grid_index
Definition: g723_1.h:113
int dirac_train
Definition: g723_1.h:83
int ad_cb_gain
Definition: g723_1.h:82
int amp_index
Definition: g723_1.h:112
static const uint8_t frame_size[4]
Definition: g723_1.h:222
G723.1 unpacked data subframe.
Definition: g723_1.h:80
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
Definition: g723_1.h:260
int min_err
Definition: g723_1.h:111
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
int index
Definition: g723_1.h:103
int ff_g723_1_normalize_bits(int num, int width)
Calculate the number of left-shifts required for normalizing the input.
Definition: g723_1.c:1120
void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
Quantize LSP frequencies by interpolation and convert them to the corresponding LPC coefficients...
Definition: g723_1.c:1251
#define FRAME_LEN
Definition: g723_1.h:37
int dirac_train
Definition: g723_1.h:114
const int16_t ff_g723_1_fixed_cb_gain[GAIN_LEVELS]
Definition: g723_1.c:453
uint8_t
Optimized fixed codebook excitation parameters.
Definition: g723_1.h:110
#define LSP_CB_SIZE
Definition: g723_1.h:42
#define LPC_ORDER
Definition: g723_1.h:40
int hpf_iir_mem
and iir memories
Definition: g723_1.h:152
Rate
G723.1 rate values.
Definition: g723_1.h:72
int pulse_sign
Definition: g723_1.h:84
#define GRID_SIZE
Definition: g723_1.h:46
int pf_gain
formant postfilter gain scaling unit memory
Definition: g723_1.h:143
FrameType
G723.1 frame types.
Definition: g723_1.h:63
int amp_index
Definition: g723_1.h:86
void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag)
Generate a train of dirac functions with period as pitch lag.
Definition: g723_1.c:1145
int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
Definition: g723_1.c:1125
GLsizei GLsizei * length
Definition: opengl_enc.c:114
int grid_index
Definition: g723_1.h:85
void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
Get delayed contribution from the previous excitation vector.
Definition: g723_1.c:1131
#define PITCH_MAX
Definition: g723_1.h:44
#define b
Definition: input.c:41
#define width
#define PULSE_MAX
Definition: g723_1.h:47
const int16_t ff_g723_1_adaptive_cb_gain85[85 *20]
Definition: g723_1.c:459
int32_t
#define HALF_FRAME_LEN
Definition: g723_1.h:38
const int16_t ff_g723_1_cos_tab[COS_TBL_SIZE+1]
Definition: g723_1.c:32
int index
postfilter backward/forward lag
Definition: g723_1.h:94
#define GAIN_LEVELS
Definition: g723_1.h:48
int16_t opt_gain
optimal gain
Definition: g723_1.h:95
int postfilter
Definition: g723_1.h:161
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
Definition: g723_1.h:227
Silence Insertion Descriptor frame.
Definition: g723_1.h:65
const int16_t ff_g723_1_lsp_band2[LSP_CB_SIZE][4]
Definition: g723_1.c:278
Describe the class of an AVClass context structure.
Definition: log.h:67
int16_t sc_gain
scaling gain
Definition: g723_1.h:96
Active speech.
Definition: g723_1.h:64
#define LSP_BANDS
Definition: g723_1.h:41
const int16_t ff_g723_1_lsp_band1[LSP_CB_SIZE][3]
Definition: g723_1.c:189
#define SUBFRAME_LEN
Definition: g723_1.h:36
void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp, uint8_t *lsp_index, int bad_frame)
Perform inverse quantization of LSP frequencies.
Definition: g723_1.c:1272
Pitch postfilter parameters.
Definition: g723_1.h:93
const int16_t ff_g723_1_lsp_band0[LSP_CB_SIZE][3]
LSP VQ tables.
Definition: g723_1.c:100
int16_t hpf_fir_mem
highpass filter fir
Definition: g723_1.h:151
Harmonic filter parameters.
Definition: g723_1.h:102
int pulse_pos
Definition: g723_1.h:87
int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length)
Scale vector contents based on the largest of their absolutes.
Definition: g723_1.c:1103
void ff_g723_1_gen_acb_excitation(int16_t *vector, int16_t *prev_excitation, int pitch_lag, G723_1_Subframe *subfrm, enum Rate cur_rate)
Generate adaptive codebook excitation.
Definition: g723_1.c:1157
const int32_t ff_g723_1_combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE]
Used for the coding/decoding of the pulses positions for the MP-MLQ codebook.
Definition: g723_1.c:409
int ad_cb_lag
adaptive codebook lag
Definition: g723_1.h:81
int gain
Definition: g723_1.h:104