]> git.sesse.net Git - ffmpeg/blob - libavcodec/g723_1.c
mpeg12dec: avoid signed overflow in bitrate calculation
[ffmpeg] / libavcodec / g723_1.c
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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     max  = FFMIN(max, 0x7FFF);
41     bits = ff_g723_1_normalize_bits(max, 15);
42
43     for (i = 0; i < length; i++)
44         dst[i] = vector[i] << bits >> 3;
45
46     return bits - 3;
47 }
48
49 int ff_g723_1_normalize_bits(int num, int width)
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_g723_1_dot_product(residual + i, cb_ptr, PITCH_ORDER);
109         vector[i] = av_sat_dadd32(1 << 15, 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] << 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] << 14) + (lpc[2] << 14);
142     f1[2] = lpc[0] * lpc[2] + (2 << 28);
143
144     f2[0] = 1 << 28;
145     f2[1] = (lpc[1] << 14) + (lpc[3] << 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]     << 16 >> i) + f1[1]) >> 1;
166         f2[1]   = ((lpc[2 * i + 1] << 16 >> 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) << 3) +
175                                                 (1 << 15)) >> 16;
176         lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
177                                                 (1 << 15)) >> 16;
178     }
179 }
180
181 void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp,
182                                int16_t *prev_lsp)
183 {
184     int i;
185     int16_t *lpc_ptr = lpc;
186
187     /* cur_lsp * 0.25 + prev_lsp * 0.75 */
188     ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
189                                  4096, 12288, 1 << 13, 14, LPC_ORDER);
190     ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
191                                  8192, 8192, 1 << 13, 14, LPC_ORDER);
192     ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
193                                  12288, 4096, 1 << 13, 14, LPC_ORDER);
194     memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
195
196     for (i = 0; i < SUBFRAMES; i++) {
197         lsp2lpc(lpc_ptr);
198         lpc_ptr += LPC_ORDER;
199     }
200 }
201
202 void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
203                              uint8_t *lsp_index, int bad_frame)
204 {
205     int min_dist, pred;
206     int i, j, temp, stable;
207
208     /* Check for frame erasure */
209     if (!bad_frame) {
210         min_dist     = 0x100;
211         pred         = 12288;
212     } else {
213         min_dist     = 0x200;
214         pred         = 23552;
215         lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
216     }
217
218     /* Get the VQ table entry corresponding to the transmitted index */
219     cur_lsp[0] = lsp_band0[lsp_index[0]][0];
220     cur_lsp[1] = lsp_band0[lsp_index[0]][1];
221     cur_lsp[2] = lsp_band0[lsp_index[0]][2];
222     cur_lsp[3] = lsp_band1[lsp_index[1]][0];
223     cur_lsp[4] = lsp_band1[lsp_index[1]][1];
224     cur_lsp[5] = lsp_band1[lsp_index[1]][2];
225     cur_lsp[6] = lsp_band2[lsp_index[2]][0];
226     cur_lsp[7] = lsp_band2[lsp_index[2]][1];
227     cur_lsp[8] = lsp_band2[lsp_index[2]][2];
228     cur_lsp[9] = lsp_band2[lsp_index[2]][3];
229
230     /* Add predicted vector & DC component to the previously quantized vector */
231     for (i = 0; i < LPC_ORDER; i++) {
232         temp        = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
233         cur_lsp[i] += dc_lsp[i] + temp;
234     }
235
236     for (i = 0; i < LPC_ORDER; i++) {
237         cur_lsp[0]             = FFMAX(cur_lsp[0], 0x180);
238         cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
239
240         /* Stability check */
241         for (j = 1; j < LPC_ORDER; j++) {
242             temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
243             if (temp > 0) {
244                 temp >>= 1;
245                 cur_lsp[j - 1] -= temp;
246                 cur_lsp[j]     += temp;
247             }
248         }
249         stable = 1;
250         for (j = 1; j < LPC_ORDER; j++) {
251             temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
252             if (temp > 0) {
253                 stable = 0;
254                 break;
255             }
256         }
257         if (stable)
258             break;
259     }
260     if (!stable)
261         memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
262 }