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 {
125  int pitch_lag[2];
127 
128  int16_t prev_lsp[LPC_ORDER];
129  int16_t sid_lsp[LPC_ORDER];
133  int16_t fir_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 */
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 
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 */
ff_g723_1_lsp_interpolate
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:1252
G723_1_ChannelContext::prev_data
int16_t prev_data[HALF_FRAME_LEN]
Definition: g723_1.h:148
PPFParam::index
int index
postfilter backward/forward lag
Definition: g723_1.h:94
frame_size
static const uint8_t frame_size[4]
Definition: g723_1.h:222
G723_1_Subframe::ad_cb_gain
int ad_cb_gain
Definition: g723_1.h:82
FRAME_LEN
#define FRAME_LEN
Definition: g723_1.h:37
G723_1_ChannelContext::interp_gain
int interp_gain
Definition: g723_1.h:139
G723_1_ChannelContext::sid_gain
int sid_gain
Definition: g723_1.h:140
ff_g723_1_adaptive_cb_gain170
const int16_t ff_g723_1_adaptive_cb_gain170[170 *20]
Definition: g723_1.c:676
ff_g723_1_gen_acb_excitation
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:1158
G723_1_ChannelContext::interp_index
int interp_index
Definition: g723_1.h:138
G723_1_ChannelContext::pitch_lag
int pitch_lag[2]
Definition: g723_1.h:125
FCBParam::amp_index
int amp_index
Definition: g723_1.h:112
Rate
Rate
G723.1 rate values.
Definition: g723_1.h:72
G723_1_Subframe::pulse_sign
int pulse_sign
Definition: g723_1.h:84
b
#define b
Definition: input.c:41
G723_1_Subframe::ad_cb_lag
int ad_cb_lag
adaptive codebook lag
Definition: g723_1.h:81
G723_1_Context
Definition: g723_1.h:159
G723_1_ChannelContext::prev_excitation
int16_t prev_excitation[PITCH_MAX]
Definition: g723_1.h:130
G723_1_Subframe::pulse_pos
int pulse_pos
Definition: g723_1.h:87
G723_1_ChannelContext::synth_mem
int16_t synth_mem[LPC_ORDER]
Definition: g723_1.h:132
G723_1_ChannelContext::hpf_fir_mem
int16_t hpf_fir_mem
highpass filter fir
Definition: g723_1.h:151
G723_1_ChannelContext::prev_weight_sig
int16_t prev_weight_sig[PITCH_MAX]
Definition: g723_1.h:149
FCBParam::grid_index
int grid_index
Definition: g723_1.h:113
GRID_SIZE
#define GRID_SIZE
Definition: g723_1.h:46
G723_1_ChannelContext::cur_frame_type
enum FrameType cur_frame_type
Definition: g723_1.h:121
ff_g723_1_fixed_cb_gain
const int16_t ff_g723_1_fixed_cb_gain[GAIN_LEVELS]
Definition: g723_1.c:454
G723_1_ChannelContext::cur_rate
enum Rate cur_rate
Definition: g723_1.h:123
ff_g723_1_normalize_bits
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:1121
LSP_CB_SIZE
#define LSP_CB_SIZE
Definition: g723_1.h:42
HFParam
Harmonic filter parameters.
Definition: g723_1.h:102
G723_1_ChannelContext::perf_fir_mem
int16_t perf_fir_mem[LPC_ORDER]
perceptual filter fir
Definition: g723_1.h:153
G723_1_ChannelContext::pf_gain
int pf_gain
formant postfilter gain scaling unit memory
Definition: g723_1.h:143
width
#define width
G723_1_ChannelContext::cng_random_seed
int cng_random_seed
Definition: g723_1.h:137
G723_1_ChannelContext::fir_mem
int16_t fir_mem[LPC_ORDER]
Definition: g723_1.h:133
FCBParam::dirac_train
int dirac_train
Definition: g723_1.h:114
G723_1_ChannelContext::random_seed
int random_seed
Definition: g723_1.h:136
G723_1_ChannelContext::audio
int16_t audio[FRAME_LEN+LPC_ORDER+PITCH_MAX+4]
Definition: g723_1.h:145
ff_g723_1_cos_tab
const int16_t ff_g723_1_cos_tab[COS_TBL_SIZE+1]
Definition: g723_1.c:33
FCBParam
Optimized fixed codebook excitation parameters.
Definition: g723_1.h:110
UNTRANSMITTED_FRAME
@ UNTRANSMITTED_FRAME
Definition: g723_1.h:66
LPC_ORDER
#define LPC_ORDER
Definition: g723_1.h:40
FrameType
FrameType
G723.1 frame types.
Definition: g723_1.h:63
FCBParam::pulse_pos
int pulse_pos[PULSE_MAX]
Definition: g723_1.h:115
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_g723_1_dot_product
int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
Definition: g723_1.c:1126
G723_1_ChannelContext::sid_lsp
int16_t sid_lsp[LPC_ORDER]
Definition: g723_1.h:129
ff_g723_1_get_residual
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:1132
RATE_6300
@ RATE_6300
Definition: g723_1.h:73
G723_1_ChannelContext::subframe
G723_1_Subframe subframe[4]
Definition: g723_1.h:120
G723_1_Subframe::amp_index
int amp_index
Definition: g723_1.h:86
FCBParam::min_err
int min_err
Definition: g723_1.h:111
G723_1_ChannelContext::past_frame_type
enum FrameType past_frame_type
Definition: g723_1.h:122
G723_1_Context::ch
G723_1_ChannelContext ch[2]
Definition: g723_1.h:163
GAIN_LEVELS
#define GAIN_LEVELS
Definition: g723_1.h:48
PULSE_MAX
#define PULSE_MAX
Definition: g723_1.h:47
FCBParam::pulse_sign
int pulse_sign[PULSE_MAX]
Definition: g723_1.h:116
ff_g723_1_gen_dirac_train
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:1146
G723_1_ChannelContext::lsp_index
uint8_t lsp_index[LSP_BANDS]
Definition: g723_1.h:124
HALF_FRAME_LEN
#define HALF_FRAME_LEN
Definition: g723_1.h:38
ACTIVE_FRAME
@ ACTIVE_FRAME
Active speech.
Definition: g723_1.h:64
SUBFRAME_LEN
#define SUBFRAME_LEN
Definition: g723_1.h:36
G723_1_ChannelContext::excitation
int16_t excitation[PITCH_MAX+FRAME_LEN+4]
Definition: g723_1.h:131
G723_1_Subframe
G723.1 unpacked data subframe.
Definition: g723_1.h:80
a
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:41
PITCH_MAX
#define PITCH_MAX
Definition: g723_1.h:44
HFParam::index
int index
Definition: g723_1.h:103
G723_1_ChannelContext::perf_iir_mem
int16_t perf_iir_mem[LPC_ORDER]
and iir memories
Definition: g723_1.h:154
G723_1_ChannelContext::reflection_coef
int reflection_coef
Definition: g723_1.h:142
ff_g723_1_adaptive_cb_gain85
const int16_t ff_g723_1_adaptive_cb_gain85[85 *20]
Definition: g723_1.c:460
log.h
ff_g723_1_lsp_band1
const int16_t ff_g723_1_lsp_band1[LSP_CB_SIZE][3]
Definition: g723_1.c:190
PPFParam::opt_gain
int16_t opt_gain
optimal gain
Definition: g723_1.h:95
ff_g723_1_lsp_band2
const int16_t ff_g723_1_lsp_band2[LSP_CB_SIZE][4]
Definition: g723_1.c:279
HFParam::gain
int gain
Definition: g723_1.h:104
G723_1_Context::postfilter
int postfilter
Definition: g723_1.h:161
ff_g723_1_lsp_band0
const int16_t ff_g723_1_lsp_band0[LSP_CB_SIZE][3]
LSP VQ tables.
Definition: g723_1.c:101
G723_1_ChannelContext::prev_lsp
int16_t prev_lsp[LPC_ORDER]
Definition: g723_1.h:128
dc_lsp
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
Definition: g723_1.h:227
RATE_5300
@ RATE_5300
Definition: g723_1.h:74
G723_1_ChannelContext::erased_frames
int erased_frames
Definition: g723_1.h:126
ff_g723_1_combinatorial_table
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:410
SID_FRAME
@ SID_FRAME
Silence Insertion Descriptor frame.
Definition: g723_1.h:65
ff_g723_1_inverse_quant
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:1273
G723_1_ChannelContext::iir_mem
int iir_mem[LPC_ORDER]
Definition: g723_1.h:134
ff_g723_1_scale_vector
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:1104
LSP_BANDS
#define LSP_BANDS
Definition: g723_1.h:41
G723_1_ChannelContext::harmonic_mem
int16_t harmonic_mem[PITCH_MAX]
Definition: g723_1.h:156
G723_1_ChannelContext::cur_gain
int cur_gain
Definition: g723_1.h:141
int32_t
int32_t
Definition: audioconvert.c:56
G723_1_Subframe::grid_index
int grid_index
Definition: g723_1.h:85
G723_1_Subframe::dirac_train
int dirac_train
Definition: g723_1.h:83
G723_1_ChannelContext::hpf_iir_mem
int hpf_iir_mem
and iir memories
Definition: g723_1.h:152
pulses
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
Definition: g723_1.h:260
G723_1_ChannelContext
Definition: g723_1.h:119
PPFParam::sc_gain
int16_t sc_gain
scaling gain
Definition: g723_1.h:96
PPFParam
Pitch postfilter parameters.
Definition: g723_1.h:93
COS_TBL_SIZE
#define COS_TBL_SIZE
Definition: g723_1.h:49