1 /*
2 * SIPR / ACELP.NET decoder
3 *
4 * Copyright (c) 2008 Vladimir Voroshilov
5 * Copyright (c) 2009 Vitor Sessak
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <math.h>
25 #include <stdint.h>
26 #include <string.h>
27
32 #define BITSTREAM_READER_LE
35
41
42 #define MAX_SUBFRAME_COUNT 5
43
46
53
54 /* bitstream parameters */
57
58 /** size in bits of the i-th stage vector of quantizer */
60
61 /** size in bits of the adaptive-codebook index for every subframe */
63
65 uint8_t fc_index_bits[10];
///< size in bits of the fixed codebook indexes
68
72 .bits_per_frame = 160,
74 .frames_per_packet = 1,
75 .pitch_sharp_factor = 0.00,
76
77 .number_of_fc_indexes = 10,
78 .ma_predictor_bits = 1,
79 .vq_indexes_bits = {7, 8, 7, 7, 7},
80 .pitch_delay_bits = {9, 6},
81 .gp_index_bits = 4,
82 .fc_index_bits = {4, 5, 4, 5, 4, 5, 4, 5, 4, 5},
83 .gc_index_bits = 5
84 },
85
87 .mode_name = "8k5",
88 .bits_per_frame = 152,
89 .subframe_count = 3,
90 .frames_per_packet = 1,
91 .pitch_sharp_factor = 0.8,
92
93 .number_of_fc_indexes = 3,
94 .ma_predictor_bits = 0,
95 .vq_indexes_bits = {6, 7, 7, 7, 5},
96 .pitch_delay_bits = {8, 5, 5},
97 .gp_index_bits = 0,
98 .fc_index_bits = {9, 9, 9},
99 .gc_index_bits = 7
100 },
101
103 .mode_name = "6k5",
104 .bits_per_frame = 232,
105 .subframe_count = 3,
106 .frames_per_packet = 2,
107 .pitch_sharp_factor = 0.8,
108
109 .number_of_fc_indexes = 3,
110 .ma_predictor_bits = 0,
111 .vq_indexes_bits = {6, 7, 7, 7, 5},
112 .pitch_delay_bits = {8, 5, 5},
113 .gp_index_bits = 0,
114 .fc_index_bits = {5, 5, 5},
115 .gc_index_bits = 7
116 },
117
119 .mode_name = "5k0",
120 .bits_per_frame = 296,
121 .subframe_count = 5,
122 .frames_per_packet = 2,
123 .pitch_sharp_factor = 0.85,
124
125 .number_of_fc_indexes = 1,
126 .ma_predictor_bits = 0,
127 .vq_indexes_bits = {6, 7, 7, 7, 5},
128 .pitch_delay_bits = {8, 5, 8, 5, 5},
129 .gp_index_bits = 0,
130 .fc_index_bits = {10},
131 .gc_index_bits = 7
132 }
133 };
134
136 1.0/(1 << 1), 1.0/(1 << 2), 1.0/(1 << 3), 1.0/(1 << 4),
137 1.0/(1 << 5), 1.0/(1 << 6), 1.0/(1 << 7), 1.0/(1 << 8),
138 1.0/(1 << 9), 1.0/(1 << 10), 1.0/(1 << 11), 1.0/(1 << 12),
139 1.0/(1 << 13), 1.0/(1 << 14), 1.0/(1 << 15), 1.0/(1 << 16)
140 };
141
142 static void dequant(
float *
out,
const int *idx,
const float *cbs[])
143 {
144 int i;
146 int num_vec = 5;
147
148 for (i = 0; i < num_vec; i++)
149 memcpy(out + stride*i, cbs[i] + stride*idx[i], stride*sizeof(float));
150
151 }
152
155 {
156 int i;
158
160
162 lsfnew[i] = lsf_history[i] * 0.33 + lsf_tmp[i] +
mean_lsf[i];
163
165
166 /* Note that a minimum distance is not enforced between the last value and
167 the previous one, contrary to what is done in ff_acelp_reorder_lsf() */
169 lsfnew[9] =
FFMIN(lsfnew[LP_FILTER_ORDER - 1], 1.3 *
M_PI);
170
171 memcpy(lsf_history, lsf_tmp, LP_FILTER_ORDER * sizeof(*lsf_history));
172
173 for (i = 0; i < LP_FILTER_ORDER - 1; i++)
174 lsfnew[i] = cos(lsfnew[i]);
175 lsfnew[LP_FILTER_ORDER - 1] *= 6.153848 /
M_PI;
176 }
177
178 /** Apply pitch lag to the fixed vector (AMR section 6.1.2). */
180 float *fixed_vector)
181 {
182 int i;
183
185 fixed_vector[i] += beta * fixed_vector[i - pitch_lag_int];
186 }
187
188 /**
189 * Extract decoding parameters from the input bitstream.
190 * @param parms parameters structure
191 * @param pgb pointer to initialized GetBitContext structure
192 */
195 {
196 int i, j;
197
200
201 for (i = 0; i < 5; i++)
203
208
211
213 }
214 }
215
217 int num_subfr)
218 {
220 int i,j;
221 float t,
t0 = 1.0 / num_subfr;
222
223 t = t0 * 0.5;
224 for (i = 0; i < num_subfr; i++) {
226 lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j];
227
231 }
232 }
233
234 /**
235 * Evaluate the adaptive impulse response.
236 */
237 static void eval_ir(
const float *Az,
int pitch_lag,
float *freq,
238 float pitch_sharp_factor)
239 {
241 int i;
242
243 tmp1[0] = 1.0;
247 }
248 memset(tmp1 + 11, 0, 37 * sizeof(float));
249
251 LP_FILTER_ORDER);
252
254 }
255
256 /**
257 * Evaluate the convolution of a vector with a sparse vector.
258 */
260 const float *shape,
int length)
261 {
262 int i, j;
263
264 memset(out, 0, length*sizeof(float));
265 for (i = 0; i < pulses->
n; i++)
266 for (j = pulses->
x[i]; j < length; j++)
267 out[j] += pulses->
y[i] * shape[j - pulses->
x[i]];
268 }
269
270 /**
271 * Apply postfilter, very similar to AMR one.
272 */
274 {
279 int i;
280
284 };
285
287 LP_FILTER_ORDER*sizeof(float));
288
290 LP_FILTER_ORDER);
291
293 LP_FILTER_ORDER*sizeof(float));
294
296
298 LP_FILTER_ORDER*sizeof(*pole_out));
299
301 LP_FILTER_ORDER*sizeof(*pole_out));
302
304 LP_FILTER_ORDER);
305
306 }
307
310 {
311 int i;
312
313 switch (mode) {
315 for (i = 0; i < 3; i++) {
316 fixed_sparse->
x[i] = 3 * (pulses[i] & 0xf) + i;
317 fixed_sparse->
y[i] = pulses[i] & 0x10 ? -1 : 1;
318 }
320 break;
322 for (i = 0; i < 3; i++) {
323 fixed_sparse->
x[2*i ] = 3 * ((pulses[i] >> 4) & 0xf) + i;
324 fixed_sparse->
x[2*i + 1] = 3 * ( pulses[i] & 0xf) + i;
325
326 fixed_sparse->
y[2*i ] = (pulses[i] & 0x100) ? -1.0: 1.0;
327
328 fixed_sparse->
y[2*i + 1] =
329 (fixed_sparse->
x[2*i + 1] < fixed_sparse->
x[2*i]) ?
330 -fixed_sparse->
y[2*i ] : fixed_sparse->
y[2*i];
331 }
332
334 break;
336 default:
337 if (low_gain) {
338 int offset = (pulses[0] & 0x200) ? 2 : 0;
340
341 for (i = 0; i < 3; i++) {
342 int index = (val & 0x7) * 6 + 4 - i*2;
343
344 fixed_sparse->
y[i] = (offset +
index) & 0x3 ? -1 : 1;
345 fixed_sparse->
x[i] =
index;
346
347 val >>= 3;
348 }
350 } else {
351 int pulse_subset = (pulses[0] >> 8) & 1;
352
353 fixed_sparse->
x[0] = ((pulses[0] >> 4) & 15) * 3 + pulse_subset;
354 fixed_sparse->
x[1] = ( pulses[0] & 15) * 3 + pulse_subset + 1;
355
356 fixed_sparse->
y[0] = pulses[0] & 0x200 ? -1 : 1;
357 fixed_sparse->
y[1] = -fixed_sparse->
y[0];
359 }
360 break;
361 }
362 }
363
365 float *out_data)
366 {
367 int i, j;
371 float *excitation;
375 float *synth = ctx->
synth_buf + 16;
// 16 instead of LP_FILTER_ORDER for
376 // memory alignment
377 int t0_first = 0;
379
380 memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float));
382
384
385 memcpy(ctx->
lsp_history, lsf_new, LP_FILTER_ORDER *
sizeof(
float));
386
388
389 for (i = 0; i < subframe_count; i++) {
392 int T0,T0_frac;
393 float pitch_gain, gain_code, avg_energy;
394
397
399 t0_first = T0;
400
403 2 * ((2 + T0_frac)%3 + 1), LP_FILTER_ORDER,
404 SUBFR_SIZE);
405
408
410
412 SUBFR_SIZE);
413
415 fixed_vector,
416 SUBFR_SIZE)) /
417 SUBFR_SIZE;
418
420
425
427 pitch_gain, gain_code, SUBFR_SIZE);
428
429 pitch_gain *= 0.5 * pitch_gain;
430 pitch_gain =
FFMIN(pitch_gain, 0.4);
431
435
437 fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j];
438
441
443 pAz, excitation, SUBFR_SIZE,
444 LP_FILTER_ORDER);
445 }
446
448 SUBFR_SIZE, LP_FILTER_ORDER);
449
451 }
452
453 memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER,
454 LP_FILTER_ORDER * sizeof(float));
455
457 for (i = 0; i < subframe_count; i++) {
460 SUBFR_SIZE);
462 &synth[i * SUBFR_SIZE], energy,
464 }
465
467 LP_FILTER_ORDER*sizeof(float));
468 }
471
473 (const float[2]) {-1.99997 , 1.000000000},
474 (const float[2]) {-1.93307352, 0.935891986},
475 0.939805806,
478 }
479
481 {
483 int i;
484
490 default:
496 "Invalid block_align: %d. Mode %s guessed based on bitrate: %d\n",
498 }
499
501
505 } else {
507 }
508
511
512 for (i = 0; i < 4; i++)
514
518
519 return 0;
520 }
521
523 int *got_frame_ptr,
AVPacket *avpkt)
524 {
531 float *samples;
534
538 "Error processing packet: packet size (%d) too small\n",
540 return -1;
541 }
542
543 /* get output buffer */
548 samples = (
float *)frame->
data[0];
549
551
554
556
558 }
559
560 *got_frame_ptr = 1;
561
563 }
564
574 };