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
31
32 #define BITSTREAM_READER_LE
42
43 #define MAX_SUBFRAME_COUNT 5
44
47
54
55 /* bitstream parameters */
58
59 /** size in bits of the i-th stage vector of quantizer */
61
62 /** size in bits of the adaptive-codebook index for every subframe */
64
66 uint8_t
fc_index_bits[10];
///< size in bits of the fixed codebook indexes
69
73 .bits_per_frame = 160,
75 .frames_per_packet = 1,
76 .pitch_sharp_factor = 0.00,
77
78 .number_of_fc_indexes = 10,
79 .ma_predictor_bits = 1,
80 .vq_indexes_bits = {7, 8, 7, 7, 7},
81 .pitch_delay_bits = {9, 6},
82 .gp_index_bits = 4,
83 .fc_index_bits = {4, 5, 4, 5, 4, 5, 4, 5, 4, 5},
84 .gc_index_bits = 5
85 },
86
88 .mode_name = "8k5",
89 .bits_per_frame = 152,
90 .subframe_count = 3,
91 .frames_per_packet = 1,
92 .pitch_sharp_factor = 0.8,
93
94 .number_of_fc_indexes = 3,
95 .ma_predictor_bits = 0,
96 .vq_indexes_bits = {6, 7, 7, 7, 5},
97 .pitch_delay_bits = {8, 5, 5},
98 .gp_index_bits = 0,
99 .fc_index_bits = {9, 9, 9},
100 .gc_index_bits = 7
101 },
102
104 .mode_name = "6k5",
105 .bits_per_frame = 232,
106 .subframe_count = 3,
107 .frames_per_packet = 2,
108 .pitch_sharp_factor = 0.8,
109
110 .number_of_fc_indexes = 3,
111 .ma_predictor_bits = 0,
112 .vq_indexes_bits = {6, 7, 7, 7, 5},
113 .pitch_delay_bits = {8, 5, 5},
114 .gp_index_bits = 0,
115 .fc_index_bits = {5, 5, 5},
116 .gc_index_bits = 7
117 },
118
120 .mode_name = "5k0",
121 .bits_per_frame = 296,
122 .subframe_count = 5,
123 .frames_per_packet = 2,
124 .pitch_sharp_factor = 0.85,
125
126 .number_of_fc_indexes = 1,
127 .ma_predictor_bits = 0,
128 .vq_indexes_bits = {6, 7, 7, 7, 5},
129 .pitch_delay_bits = {8, 5, 8, 5, 5},
130 .gp_index_bits = 0,
131 .fc_index_bits = {10},
132 .gc_index_bits = 7
133 }
134 };
135
137 1.0/(1 << 1), 1.0/(1 << 2), 1.0/(1 << 3), 1.0/(1 << 4),
138 1.0/(1 << 5), 1.0/(1 << 6), 1.0/(1 << 7), 1.0/(1 << 8),
139 1.0/(1 << 9), 1.0/(1 << 10), 1.0/(1 << 11), 1.0/(1 << 12),
140 1.0/(1 << 13), 1.0/(1 << 14), 1.0/(1 << 15), 1.0/(1 << 16)
141 };
142
143 static void dequant(
float *
out,
const int *idx,
const float *
const cbs[])
144 {
147 int num_vec = 5;
148
149 for (
i = 0;
i < num_vec;
i++)
151
152 }
153
156 {
159
161
163 lsfnew[
i] = lsf_history[
i] * 0.33 + lsf_tmp[
i] +
mean_lsf[
i];
164
166
167 /* Note that a minimum distance is not enforced between the last value and
168 the previous one, contrary to what is done in ff_acelp_reorder_lsf() */
171
173
175 lsfnew[
i] = cos(lsfnew[
i]);
177 }
178
179 /** Apply pitch lag to the fixed vector (AMR section 6.1.2). */
181 float *fixed_vector)
182 {
184
186 fixed_vector[
i] += beta * fixed_vector[
i - pitch_lag_int];
187 }
188
189 /**
190 * Extract decoding parameters from the input bitstream.
191 * @param parms parameters structure
192 * @param pgb pointer to initialized GetBitContext structure
193 */
196 {
198
201
202 for (
i = 0;
i < 5;
i++)
204
209
212
214 }
215 }
216
218 int num_subfr)
219 {
222 float t, t0 = 1.0 / num_subfr;
223
224 t = t0 * 0.5;
225 for (
i = 0;
i < num_subfr;
i++) {
227 lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j];
228
231 t += t0;
232 }
233 }
234
235 /**
236 * Evaluate the adaptive impulse response.
237 */
238 static void eval_ir(
const float *Az,
int pitch_lag,
float *freq,
239 float pitch_sharp_factor)
240 {
243
244 tmp1[0] = 1.0;
248 }
249 memset(tmp1 + 11, 0, 37 * sizeof(float));
250
253
255 }
256
257 /**
258 * Evaluate the convolution of a vector with a sparse vector.
259 */
261 const float *shape, int length)
262 {
264
265 memset(
out, 0, length*
sizeof(
float));
267 for (j =
pulses->x[
i]; j < length; j++)
269 }
270
271 /**
272 * Apply postfilter, very similar to AMR one.
273 */
275 {
281
285 };
286
289
292
295
297
300
303
306
307 }
308
311 {
313
316 for (
i = 0;
i < 3;
i++) {
317 fixed_sparse->
x[
i] = 3 * (
pulses[
i] & 0xf) +
i;
318 fixed_sparse->
y[
i] =
pulses[
i] & 0x10 ? -1 : 1;
319 }
321 break;
323 for (
i = 0;
i < 3;
i++) {
324 fixed_sparse->
x[2*
i ] = 3 * ((
pulses[
i] >> 4) & 0
xf) +
i;
325 fixed_sparse->
x[2*
i + 1] = 3 * (
pulses[
i] & 0xf) +
i;
326
327 fixed_sparse->
y[2*
i ] = (
pulses[
i] & 0x100) ? -1.0: 1.0;
328
329 fixed_sparse->
y[2*
i + 1] =
330 (fixed_sparse->
x[2*
i + 1] < fixed_sparse->
x[2*
i]) ?
331 -fixed_sparse->
y[2*
i ] : fixed_sparse->
y[2*
i];
332 }
333
335 break;
337 default:
338 if (low_gain) {
341
342 for (
i = 0;
i < 3;
i++) {
344
347
349 }
351 } else {
352 int pulse_subset = (
pulses[0] >> 8) & 1;
353
354 fixed_sparse->
x[0] = ((
pulses[0] >> 4) & 15) * 3 + pulse_subset;
355 fixed_sparse->
x[1] = (
pulses[0] & 15) * 3 + pulse_subset + 1;
356
357 fixed_sparse->
y[0] =
pulses[0] & 0x200 ? -1 : 1;
358 fixed_sparse->
y[1] = -fixed_sparse->
y[0];
360 }
361 break;
362 }
363 }
364
366 float *out_data)
367 {
372 float *excitation;
376 float *synth =
ctx->synth_buf + 16;
// 16 instead of LP_FILTER_ORDER for
377 // memory alignment
378 int t0_first = 0;
380
383
385
387
389
390 for (
i = 0;
i < subframe_count;
i++) {
393 int T0,T0_frac;
394 float pitch_gain, gain_code, avg_energy;
395
398
400 t0_first = T0;
401
406
408 ctx->past_pitch_gain < 0.8);
409
411
414
416 fixed_vector,
419
421
423 avg_energy,
ctx->energy_history,
426
429
430 pitch_gain *= 0.5 * pitch_gain;
431 pitch_gain =
FFMIN(pitch_gain, 0.4);
432
433 ctx->gain_mem = 0.7 *
ctx->gain_mem + 0.3 * pitch_gain;
435 gain_code *=
ctx->gain_mem;
436
438 fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j];
439
442
446 }
447
450
452 }
453
456
458 for (
i = 0;
i < subframe_count;
i++) {
465 }
466
469 }
472
474 (const float[2]) {-1.99997 , 1.000000000},
475 (const float[2]) {-1.93307352, 0.935891986},
476 0.939805806,
477 ctx->highpass_filt_mem,
479 }
480
482 {
485
491 default:
497 "Invalid block_align: %d. Mode %s guessed based on bitrate: %"PRId64"\n",
499 }
500
502
506 } else {
508 }
509
512
513 for (
i = 0;
i < 4;
i++)
514 ctx->energy_history[
i] = -14;
515
519
520 return 0;
521 }
522
524 int *got_frame_ptr,
AVPacket *avpkt)
525 {
527 const uint8_t *buf=avpkt->
data;
534
537 "Error processing packet: packet size (%d) too small\n",
540 }
541
542 /* get output buffer */
548
550
553
555
557 }
558
559 *got_frame_ptr = 1;
560
562 }
563
573 };