FFmpeg
g723_1.c
Go to the documentation of this file.
1 /*
2  * G.723.1 compatible decoder
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 #include <stdint.h>
24 
25 #include "libavutil/common.h"
26 
27 #include "acelp_vectors.h"
28 #include "avcodec.h"
29 #include "celp_math.h"
30 #include "g723_1.h"
31 
32 int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length)
33 {
34  int bits, max = 0;
35  int i;
36 
37  for (i = 0; i < length; i++)
38  max |= FFABS(vector[i]);
39 
40  bits= 14 - av_log2_16bit(max);
41  bits= FFMAX(bits, 0);
42 
43  for (i = 0; i < length; i++)
44  dst[i] = (vector[i] * (1 << bits)) >> 3;
45 
46  return bits - 3;
47 }
48 
50 {
51  return width - av_log2(num) - 1;
52 }
53 
54 int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
55 {
56  int sum = ff_dot_product(a, b, length);
57  return av_sat_add32(sum, sum);
58 }
59 
60 void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation,
61  int lag)
62 {
63  int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
64  int i;
65 
66  residual[0] = prev_excitation[offset];
67  residual[1] = prev_excitation[offset + 1];
68 
69  offset += 2;
70  for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
71  residual[i] = prev_excitation[offset + (i - 2) % lag];
72 }
73 
74 void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag)
75 {
76  int16_t vector[SUBFRAME_LEN];
77  int i, j;
78 
79  memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
80  for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
81  for (j = 0; j < SUBFRAME_LEN - i; j++)
82  buf[i + j] += vector[j];
83  }
84 }
85 
86 void ff_g723_1_gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
87  int pitch_lag, G723_1_Subframe *subfrm,
88  enum Rate cur_rate)
89 {
90  int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
91  const int16_t *cb_ptr;
92  int lag = pitch_lag + subfrm->ad_cb_lag - 1;
93 
94  int i;
95  int sum;
96 
97  ff_g723_1_get_residual(residual, prev_excitation, lag);
98 
99  /* Select quantization table */
100  if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2) {
101  cb_ptr = adaptive_cb_gain85;
102  } else
103  cb_ptr = adaptive_cb_gain170;
104 
105  /* Calculate adaptive vector */
106  cb_ptr += subfrm->ad_cb_gain * 20;
107  for (i = 0; i < SUBFRAME_LEN; i++) {
108  sum = ff_dot_product(residual + i, cb_ptr, PITCH_ORDER);
109  vector[i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
110  }
111 }
112 
113 /**
114  * Convert LSP frequencies to LPC coefficients.
115  *
116  * @param lpc buffer for LPC coefficients
117  */
118 static void lsp2lpc(int16_t *lpc)
119 {
120  int f1[LPC_ORDER / 2 + 1];
121  int f2[LPC_ORDER / 2 + 1];
122  int i, j;
123 
124  /* Calculate negative cosine */
125  for (j = 0; j < LPC_ORDER; j++) {
126  int index = (lpc[j] >> 7) & 0x1FF;
127  int offset = lpc[j] & 0x7f;
128  int temp1 = cos_tab[index] * (1 << 16);
129  int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
130  (((offset << 8) + 0x80) << 1);
131 
132  lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
133  }
134 
135  /*
136  * Compute sum and difference polynomial coefficients
137  * (bitexact alternative to lsp2poly() in lsp.c)
138  */
139  /* Initialize with values in Q28 */
140  f1[0] = 1 << 28;
141  f1[1] = (lpc[0] + lpc[2]) * (1 << 14);
142  f1[2] = lpc[0] * lpc[2] + (2 << 28);
143 
144  f2[0] = 1 << 28;
145  f2[1] = (lpc[1] + lpc[3]) * (1 << 14);
146  f2[2] = lpc[1] * lpc[3] + (2 << 28);
147 
148  /*
149  * Calculate and scale the coefficients by 1/2 in
150  * each iteration for a final scaling factor of Q25
151  */
152  for (i = 2; i < LPC_ORDER / 2; i++) {
153  f1[i + 1] = av_clipl_int32(f1[i - 1] + (int64_t)MULL2(f1[i], lpc[2 * i]));
154  f2[i + 1] = av_clipl_int32(f2[i - 1] + (int64_t)MULL2(f2[i], lpc[2 * i + 1]));
155 
156  for (j = i; j >= 2; j--) {
157  f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
158  (f1[j] >> 1) + (f1[j - 2] >> 1);
159  f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
160  (f2[j] >> 1) + (f2[j - 2] >> 1);
161  }
162 
163  f1[0] >>= 1;
164  f2[0] >>= 1;
165  f1[1] = ((lpc[2 * i] * 65536 >> i) + f1[1]) >> 1;
166  f2[1] = ((lpc[2 * i + 1] * 65536 >> i) + f2[1]) >> 1;
167  }
168 
169  /* Convert polynomial coefficients to LPC coefficients */
170  for (i = 0; i < LPC_ORDER / 2; i++) {
171  int64_t ff1 = f1[i + 1] + f1[i];
172  int64_t ff2 = f2[i + 1] - f2[i];
173 
174  lpc[i] = av_clipl_int32(((ff1 + ff2) * 8) + (1 << 15)) >> 16;
175  lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) * 8) +
176  (1 << 15)) >> 16;
177  }
178 }
179 
180 void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp,
181  int16_t *prev_lsp)
182 {
183  int i;
184  int16_t *lpc_ptr = lpc;
185 
186  /* cur_lsp * 0.25 + prev_lsp * 0.75 */
187  ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
188  4096, 12288, 1 << 13, 14, LPC_ORDER);
189  ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
190  8192, 8192, 1 << 13, 14, LPC_ORDER);
191  ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
192  12288, 4096, 1 << 13, 14, LPC_ORDER);
193  memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
194 
195  for (i = 0; i < SUBFRAMES; i++) {
196  lsp2lpc(lpc_ptr);
197  lpc_ptr += LPC_ORDER;
198  }
199 }
200 
201 void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
202  uint8_t *lsp_index, int bad_frame)
203 {
204  int min_dist, pred;
205  int i, j, temp, stable;
206 
207  /* Check for frame erasure */
208  if (!bad_frame) {
209  min_dist = 0x100;
210  pred = 12288;
211  } else {
212  min_dist = 0x200;
213  pred = 23552;
214  lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
215  }
216 
217  /* Get the VQ table entry corresponding to the transmitted index */
218  cur_lsp[0] = lsp_band0[lsp_index[0]][0];
219  cur_lsp[1] = lsp_band0[lsp_index[0]][1];
220  cur_lsp[2] = lsp_band0[lsp_index[0]][2];
221  cur_lsp[3] = lsp_band1[lsp_index[1]][0];
222  cur_lsp[4] = lsp_band1[lsp_index[1]][1];
223  cur_lsp[5] = lsp_band1[lsp_index[1]][2];
224  cur_lsp[6] = lsp_band2[lsp_index[2]][0];
225  cur_lsp[7] = lsp_band2[lsp_index[2]][1];
226  cur_lsp[8] = lsp_band2[lsp_index[2]][2];
227  cur_lsp[9] = lsp_band2[lsp_index[2]][3];
228 
229  /* Add predicted vector & DC component to the previously quantized vector */
230  for (i = 0; i < LPC_ORDER; i++) {
231  temp = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
232  cur_lsp[i] += dc_lsp[i] + temp;
233  }
234 
235  for (i = 0; i < LPC_ORDER; i++) {
236  cur_lsp[0] = FFMAX(cur_lsp[0], 0x180);
237  cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
238 
239  /* Stability check */
240  for (j = 1; j < LPC_ORDER; j++) {
241  temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
242  if (temp > 0) {
243  temp >>= 1;
244  cur_lsp[j - 1] -= temp;
245  cur_lsp[j] += temp;
246  }
247  }
248  stable = 1;
249  for (j = 1; j < LPC_ORDER; j++) {
250  temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
251  if (temp > 0) {
252  stable = 0;
253  break;
254  }
255  }
256  if (stable)
257  break;
258  }
259  if (!stable)
260  memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
261 }
acelp_vectors.h
G723_1_Subframe::ad_cb_gain
int ad_cb_gain
Definition: g723_1.h:82
lsp_band0
static const int16_t lsp_band0[LSP_CB_SIZE][3]
LSP VQ tables.
Definition: g723_1.h:317
av_log2_16bit
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
Rate
Rate
G723.1 rate values.
Definition: g723_1.h:72
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
max
#define max(a, b)
Definition: cuda_runtime.h:33
lsp2lpc
static void lsp2lpc(int16_t *lpc)
Convert LSP frequencies to LPC coefficients.
Definition: g723_1.c:118
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:201
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:49
adaptive_cb_gain85
static const int16_t adaptive_cb_gain85[85 *20]
Definition: g723_1.h:736
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:74
PITCH_ORDER
#define PITCH_ORDER
Definition: g723_1.h:45
lsp_band1
static const int16_t lsp_band1[LSP_CB_SIZE][3]
Definition: g723_1.h:406
adaptive_cb_gain170
static const int16_t adaptive_cb_gain170[170 *20]
Definition: g723_1.h:952
buf
void * buf
Definition: avisynth_c.h:766
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:86
width
#define width
bits
uint8_t bits
Definition: vp3data.h:202
lsp_band2
static const int16_t lsp_band2[LSP_CB_SIZE][4]
Definition: g723_1.h:495
LPC_ORDER
#define LPC_ORDER
Definition: g723_1.h:40
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
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:180
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:60
RATE_6300
@ RATE_6300
Definition: g723_1.h:73
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:54
index
int index
Definition: gxfenc.c:89
SUBFRAMES
#define SUBFRAMES
Definition: dcaenc.c:50
g723_1.h
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
celp_math.h
SUBFRAME_LEN
#define SUBFRAME_LEN
Definition: g723_1.h:36
G723_1_Subframe
G723.1 unpacked data subframe.
Definition: g723_1.h:80
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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
residual
uint64_t residual
Definition: dirac_vlc.h:29
PITCH_MAX
#define PITCH_MAX
Definition: g723_1.h:44
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
ff_dot_product
int64_t ff_dot_product(const int16_t *a, const int16_t *b, int length)
Calculate the dot product of 2 int16_t vectors.
Definition: celp_math.c:98
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:32
common.h
uint8_t
uint8_t
Definition: audio_convert.c:194
avcodec.h
pred
static const float pred[4]
Definition: siprdata.h:259
dc_lsp
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
Definition: g723_1.h:232
temp
else temp
Definition: vf_mcdeint.c:256
ff_acelp_weighted_vector_sum
void ff_acelp_weighted_vector_sum(int16_t *out, const int16_t *in_a, const int16_t *in_b, int16_t weight_coeff_a, int16_t weight_coeff_b, int16_t rounder, int shift, int length)
weighted sum of two vectors with rounding.
Definition: acelp_vectors.c:173
length
const char int length
Definition: avisynth_c.h:860
MULL2
#define MULL2(a, b)
Bitexact implementation of 2ab scaled by 1/2^16.
Definition: g723_1.h:57
cos_tab
static float cos_tab[256]
Definition: dca_lbr.c:123
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26