GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/g723_1.c Lines: 119 122 97.5 %
Date: 2019-11-20 04:07:19 Branches: 40 44 90.9 %

Line Branch Exec Source
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
3951
int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length)
33
{
34
3951
    int bits, max = 0;
35
    int i;
36
37
588886
    for (i = 0; i < length; i++)
38
584935
        max |= FFABS(vector[i]);
39
40
3951
    bits= 14 - av_log2_16bit(max);
41
3951
    bits= FFMAX(bits, 0);
42
43
588886
    for (i = 0; i < length; i++)
44
584935
        dst[i] = (vector[i] * (1 << bits)) >> 3;
45
46
3951
    return bits - 3;
47
}
48
49
87014
int ff_g723_1_normalize_bits(int num, int width)
50
{
51
87014
    return width - av_log2(num) - 1;
52
}
53
54
547022
int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
55
{
56
547022
    int sum = ff_dot_product(a, b, length);
57
547022
    return av_sat_add32(sum, sum);
58
}
59
60
6896
void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation,
61
                            int lag)
62
{
63
6896
    int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
64
    int i;
65
66
6896
    residual[0] = prev_excitation[offset];
67
6896
    residual[1] = prev_excitation[offset + 1];
68
69
6896
    offset += 2;
70
434448
    for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
71
427552
        residual[i] = prev_excitation[offset + (i - 2) % lag];
72
6896
}
73
74
2197
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
2197
    memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
80
5879
    for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
81
90748
        for (j = 0; j < SUBFRAME_LEN - i; j++)
82
87066
            buf[i + j] += vector[j];
83
    }
84
2197
}
85
86
4096
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
4096
    int lag = pitch_lag + subfrm->ad_cb_lag - 1;
93
94
    int i;
95
    int sum;
96
97
4096
    ff_g723_1_get_residual(residual, prev_excitation, lag);
98
99
    /* Select quantization table */
100

4096
    if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2) {
101
3258
        cb_ptr = adaptive_cb_gain85;
102
    } else
103
838
        cb_ptr = adaptive_cb_gain170;
104
105
    /* Calculate adaptive vector */
106
4096
    cb_ptr += subfrm->ad_cb_gain * 20;
107
249856
    for (i = 0; i < SUBFRAME_LEN; i++) {
108
245760
        sum = ff_dot_product(residual + i, cb_ptr, PITCH_ORDER);
109
245760
        vector[i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
110
    }
111
4096
}
112
113
/**
114
 * Convert LSP frequencies to LPC coefficients.
115
 *
116
 * @param lpc buffer for LPC coefficients
117
 */
118
3304
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
36344
    for (j = 0; j < LPC_ORDER; j++) {
126
33040
        int index     = (lpc[j] >> 7) & 0x1FF;
127
33040
        int offset    = lpc[j] & 0x7f;
128
33040
        int temp1     = cos_tab[index] * (1 << 16);
129
33040
        int temp2     = (cos_tab[index + 1] - cos_tab[index]) *
130
33040
                          (((offset << 8) + 0x80) << 1);
131
132
33040
        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
3304
    f1[0] = 1 << 28;
141
3304
    f1[1] = (lpc[0] + lpc[2]) * (1 << 14);
142
3304
    f1[2] = lpc[0] * lpc[2] + (2 << 28);
143
144
3304
    f2[0] = 1 << 28;
145
3304
    f2[1] = (lpc[1] + lpc[3]) * (1 << 14);
146
3304
    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
13216
    for (i = 2; i < LPC_ORDER / 2; i++) {
153
9912
        f1[i + 1] = av_clipl_int32(f1[i - 1] + (int64_t)MULL2(f1[i], lpc[2 * i]));
154
9912
        f2[i + 1] = av_clipl_int32(f2[i - 1] + (int64_t)MULL2(f2[i], lpc[2 * i + 1]));
155
156
29736
        for (j = i; j >= 2; j--) {
157
19824
            f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
158
19824
                    (f1[j] >> 1) + (f1[j - 2] >> 1);
159
19824
            f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
160
19824
                    (f2[j] >> 1) + (f2[j - 2] >> 1);
161
        }
162
163
9912
        f1[0] >>= 1;
164
9912
        f2[0] >>= 1;
165
9912
        f1[1] = ((lpc[2 * i]     * 65536 >> i) + f1[1]) >> 1;
166
9912
        f2[1] = ((lpc[2 * i + 1] * 65536 >> i) + f2[1]) >> 1;
167
    }
168
169
    /* Convert polynomial coefficients to LPC coefficients */
170
19824
    for (i = 0; i < LPC_ORDER / 2; i++) {
171
16520
        int64_t ff1 = f1[i + 1] + f1[i];
172
16520
        int64_t ff2 = f2[i + 1] - f2[i];
173
174
16520
        lpc[i] = av_clipl_int32(((ff1 + ff2) * 8) + (1 << 15)) >> 16;
175
16520
        lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) * 8) +
176
16520
                                                (1 << 15)) >> 16;
177
    }
178
3304
}
179
180
826
void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp,
181
                               int16_t *prev_lsp)
182
{
183
    int i;
184
826
    int16_t *lpc_ptr = lpc;
185
186
    /* cur_lsp * 0.25 + prev_lsp * 0.75 */
187
826
    ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
188
                                 4096, 12288, 1 << 13, 14, LPC_ORDER);
189
826
    ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
190
                                 8192, 8192, 1 << 13, 14, LPC_ORDER);
191
826
    ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
192
                                 12288, 4096, 1 << 13, 14, LPC_ORDER);
193
826
    memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
194
195
4130
    for (i = 0; i < SUBFRAMES; i++) {
196
3304
        lsp2lpc(lpc_ptr);
197
3304
        lpc_ptr += LPC_ORDER;
198
    }
199
826
}
200
201
784
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
784
    if (!bad_frame) {
209
782
        min_dist     = 0x100;
210
782
        pred         = 12288;
211
    } else {
212
2
        min_dist     = 0x200;
213
2
        pred         = 23552;
214
2
        lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
215
    }
216
217
    /* Get the VQ table entry corresponding to the transmitted index */
218
784
    cur_lsp[0] = lsp_band0[lsp_index[0]][0];
219
784
    cur_lsp[1] = lsp_band0[lsp_index[0]][1];
220
784
    cur_lsp[2] = lsp_band0[lsp_index[0]][2];
221
784
    cur_lsp[3] = lsp_band1[lsp_index[1]][0];
222
784
    cur_lsp[4] = lsp_band1[lsp_index[1]][1];
223
784
    cur_lsp[5] = lsp_band1[lsp_index[1]][2];
224
784
    cur_lsp[6] = lsp_band2[lsp_index[2]][0];
225
784
    cur_lsp[7] = lsp_band2[lsp_index[2]][1];
226
784
    cur_lsp[8] = lsp_band2[lsp_index[2]][2];
227
784
    cur_lsp[9] = lsp_band2[lsp_index[2]][3];
228
229
    /* Add predicted vector & DC component to the previously quantized vector */
230
8624
    for (i = 0; i < LPC_ORDER; i++) {
231
7840
        temp        = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
232
7840
        cur_lsp[i] += dc_lsp[i] + temp;
233
    }
234
235
784
    for (i = 0; i < LPC_ORDER; i++) {
236
784
        cur_lsp[0]             = FFMAX(cur_lsp[0],  0x180);
237
784
        cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
238
239
        /* Stability check */
240
7840
        for (j = 1; j < LPC_ORDER; j++) {
241
7056
            temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
242
7056
            if (temp > 0) {
243
150
                temp >>= 1;
244
150
                cur_lsp[j - 1] -= temp;
245
150
                cur_lsp[j]     += temp;
246
            }
247
        }
248
784
        stable = 1;
249
7840
        for (j = 1; j < LPC_ORDER; j++) {
250
7056
            temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
251
7056
            if (temp > 0) {
252
                stable = 0;
253
                break;
254
            }
255
        }
256
784
        if (stable)
257
784
            break;
258
    }
259
784
    if (!stable)
260
        memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
261
784
}