FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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] = f1[i - 1] + MULL2(f1[i], lpc[2 * i]);
154  f2[i + 1] = f2[i - 1] + 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 }
static void lsp2lpc(int16_t *lpc)
Convert LSP frequencies to LPC coefficients.
Definition: g723_1.c:118
int ad_cb_gain
Definition: g723_1.h:82
else temp
Definition: vf_mcdeint.c:259
G723.1 unpacked data subframe.
Definition: g723_1.h:80
static float cos_tab[256]
Definition: dca_lbr.c:128
const char * b
Definition: vf_curves.c:113
#define PITCH_ORDER
Definition: g723_1.h:45
int av_log2(unsigned v)
Definition: intmath.c:26
static const int16_t lsp_band0[LSP_CB_SIZE][3]
LSP VQ tables.
Definition: g723_1.h:314
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
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
uint8_t bits
Definition: crc.c:296
uint8_t
#define LPC_ORDER
Definition: g723_1.h:40
Rate
G723.1 rate values.
Definition: g723_1.h:72
static const int16_t adaptive_cb_gain85[85 *20]
Definition: g723_1.h:733
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
static const int16_t lsp_band2[LSP_CB_SIZE][4]
Definition: g723_1.h:492
static const int16_t lsp_band1[LSP_CB_SIZE][3]
Definition: g723_1.h:403
static const int16_t adaptive_cb_gain170[170 *20]
Definition: g723_1.h:949
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
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
GLsizei GLsizei * length
Definition: opengl_enc.c:115
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
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
uint64_t residual
Definition: dirac_vlc.h:29
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
#define PITCH_MAX
Definition: g723_1.h:44
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.
#define FFMIN(a, b)
Definition: common.h:96
#define width
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
int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
Definition: g723_1.c:54
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static const float pred[4]
Definition: siprdata.h:259
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
Libavcodec external API header.
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
Definition: g723_1.h:229
G.723.1 types, functions and data tables.
void * buf
Definition: avisynth_c.h:690
int index
Definition: gxfenc.c:89
#define SUBFRAME_LEN
Definition: g723_1.h:36
#define SUBFRAMES
Definition: dcaenc.c:42
common internal and external API header
#define MULL2(a, b)
Bitexact implementation of 2ab scaled by 1/2^16.
Definition: g723_1.h:57
int ad_cb_lag
adaptive codebook lag
Definition: g723_1.h:81