| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 28 | #include "libavutil/channel_layout.h" | ||
| 29 | #include "libavutil/float_dsp.h" | ||
| 30 | #include "libavutil/mathematics.h" | ||
| 31 | |||
| 32 | #define BITSTREAM_READER_LE | ||
| 33 | #include "avcodec.h" | ||
| 34 | #include "codec_internal.h" | ||
| 35 | #include "decode.h" | ||
| 36 | #include "get_bits.h" | ||
| 37 | #include "lsp.h" | ||
| 38 | #include "acelp_vectors.h" | ||
| 39 | #include "acelp_pitch_delay.h" | ||
| 40 | #include "acelp_filters.h" | ||
| 41 | #include "celp_filters.h" | ||
| 42 | |||
| 43 | #define MAX_SUBFRAME_COUNT 5 | ||
| 44 | |||
| 45 | #include "sipr.h" | ||
| 46 | #include "siprdata.h" | ||
| 47 | |||
| 48 | typedef struct SiprModeParam { | ||
| 49 | const char *mode_name; | ||
| 50 | uint16_t bits_per_frame; | ||
| 51 | uint8_t subframe_count; | ||
| 52 | uint8_t frames_per_packet; | ||
| 53 | float pitch_sharp_factor; | ||
| 54 | |||
| 55 | /* bitstream parameters */ | ||
| 56 | uint8_t number_of_fc_indexes; | ||
| 57 | uint8_t ma_predictor_bits; ///< size in bits of the switched MA predictor | ||
| 58 | |||
| 59 | /** size in bits of the i-th stage vector of quantizer */ | ||
| 60 | uint8_t vq_indexes_bits[5]; | ||
| 61 | |||
| 62 | /** size in bits of the adaptive-codebook index for every subframe */ | ||
| 63 | uint8_t pitch_delay_bits[5]; | ||
| 64 | |||
| 65 | uint8_t gp_index_bits; | ||
| 66 | uint8_t fc_index_bits[10]; ///< size in bits of the fixed codebook indexes | ||
| 67 | uint8_t gc_index_bits; ///< size in bits of the gain codebook indexes | ||
| 68 | } SiprModeParam; | ||
| 69 | |||
| 70 | static const SiprModeParam modes[MODE_COUNT] = { | ||
| 71 | [MODE_16k] = { | ||
| 72 | .mode_name = "16k", | ||
| 73 | .bits_per_frame = 160, | ||
| 74 | .subframe_count = SUBFRAME_COUNT_16k, | ||
| 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 | |||
| 87 | [MODE_8k5] = { | ||
| 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 | |||
| 103 | [MODE_6k5] = { | ||
| 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 | |||
| 119 | [MODE_5k0] = { | ||
| 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 | |||
| 136 | const float ff_pow_0_5[] = { | ||
| 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 | 5190 | static void dequant(float *out, const int *idx, const float * const cbs[]) | |
| 144 | { | ||
| 145 | int i; | ||
| 146 | 5190 | int stride = 2; | |
| 147 | 5190 | int num_vec = 5; | |
| 148 | |||
| 149 |
2/2✓ Branch 0 taken 25950 times.
✓ Branch 1 taken 5190 times.
|
31140 | for (i = 0; i < num_vec; i++) |
| 150 | 25950 | memcpy(out + stride*i, cbs[i] + stride*idx[i], stride*sizeof(float)); | |
| 151 | |||
| 152 | 5190 | } | |
| 153 | |||
| 154 | 5190 | static void lsf_decode_fp(float *lsfnew, float *lsf_history, | |
| 155 | const SiprParameters *parm) | ||
| 156 | { | ||
| 157 | int i; | ||
| 158 | float lsf_tmp[LP_FILTER_ORDER]; | ||
| 159 | |||
| 160 | 5190 | dequant(lsf_tmp, parm->vq_indexes, lsf_codebooks); | |
| 161 | |||
| 162 |
2/2✓ Branch 0 taken 51900 times.
✓ Branch 1 taken 5190 times.
|
57090 | for (i = 0; i < LP_FILTER_ORDER; i++) |
| 163 | 51900 | lsfnew[i] = lsf_history[i] * 0.33 + lsf_tmp[i] + mean_lsf[i]; | |
| 164 | |||
| 165 | 5190 | ff_sort_nearly_sorted_floats(lsfnew, LP_FILTER_ORDER - 1); | |
| 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() */ | ||
| 169 | 5190 | ff_set_min_dist_lsf(lsfnew, LSFQ_DIFF_MIN, LP_FILTER_ORDER - 1); | |
| 170 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5190 times.
|
5190 | lsfnew[9] = FFMIN(lsfnew[LP_FILTER_ORDER - 1], 1.3 * M_PI); |
| 171 | |||
| 172 | 5190 | memcpy(lsf_history, lsf_tmp, LP_FILTER_ORDER * sizeof(*lsf_history)); | |
| 173 | |||
| 174 |
2/2✓ Branch 0 taken 46710 times.
✓ Branch 1 taken 5190 times.
|
51900 | for (i = 0; i < LP_FILTER_ORDER - 1; i++) |
| 175 | 46710 | lsfnew[i] = cos(lsfnew[i]); | |
| 176 | 5190 | lsfnew[LP_FILTER_ORDER - 1] *= 6.153848 / M_PI; | |
| 177 | 5190 | } | |
| 178 | |||
| 179 | /** Apply pitch lag to the fixed vector (AMR section 6.1.2). */ | ||
| 180 | 16918 | static void pitch_sharpening(int pitch_lag_int, float beta, | |
| 181 | float *fixed_vector) | ||
| 182 | { | ||
| 183 | int i; | ||
| 184 | |||
| 185 |
2/2✓ Branch 0 taken 71279 times.
✓ Branch 1 taken 16918 times.
|
88197 | for (i = pitch_lag_int; i < SUBFR_SIZE; i++) |
| 186 | 71279 | fixed_vector[i] += beta * fixed_vector[i - pitch_lag_int]; | |
| 187 | 16918 | } | |
| 188 | |||
| 189 | /** | ||
| 190 | * Extract decoding parameters from the input bitstream. | ||
| 191 | * @param parms parameters structure | ||
| 192 | * @param pgb pointer to initialized GetBitContext structure | ||
| 193 | */ | ||
| 194 | 8445 | static void decode_parameters(SiprParameters* parms, GetBitContext *pgb, | |
| 195 | const SiprModeParam *p) | ||
| 196 | { | ||
| 197 | int i, j; | ||
| 198 | |||
| 199 |
2/2✓ Branch 0 taken 3255 times.
✓ Branch 1 taken 5190 times.
|
8445 | if (p->ma_predictor_bits) |
| 200 | 3255 | parms->ma_pred_switch = get_bits(pgb, p->ma_predictor_bits); | |
| 201 | |||
| 202 |
2/2✓ Branch 0 taken 42225 times.
✓ Branch 1 taken 8445 times.
|
50670 | for (i = 0; i < 5; i++) |
| 203 | 42225 | parms->vq_indexes[i] = get_bits(pgb, p->vq_indexes_bits[i]); | |
| 204 | |||
| 205 |
2/2✓ Branch 0 taken 23428 times.
✓ Branch 1 taken 8445 times.
|
31873 | for (i = 0; i < p->subframe_count; i++) { |
| 206 | 23428 | parms->pitch_delay[i] = get_bits(pgb, p->pitch_delay_bits[i]); | |
| 207 |
2/2✓ Branch 0 taken 6510 times.
✓ Branch 1 taken 16918 times.
|
23428 | if (p->gp_index_bits) |
| 208 | 6510 | parms->gp_index[i] = get_bits(pgb, p->gp_index_bits); | |
| 209 | |||
| 210 |
2/2✓ Branch 0 taken 109114 times.
✓ Branch 1 taken 23428 times.
|
132542 | for (j = 0; j < p->number_of_fc_indexes; j++) |
| 211 | 109114 | parms->fc_indexes[i][j] = get_bits(pgb, p->fc_index_bits[j]); | |
| 212 | |||
| 213 | 23428 | parms->gc_index[i] = get_bits(pgb, p->gc_index_bits); | |
| 214 | } | ||
| 215 | 8445 | } | |
| 216 | |||
| 217 | 5190 | static void sipr_decode_lp(float *lsfnew, const float *lsfold, float *Az, | |
| 218 | int num_subfr) | ||
| 219 | { | ||
| 220 | double lsfint[LP_FILTER_ORDER]; | ||
| 221 | int i,j; | ||
| 222 | 5190 | float t, t0 = 1.0 / num_subfr; | |
| 223 | |||
| 224 | 5190 | t = t0 * 0.5; | |
| 225 |
2/2✓ Branch 0 taken 16918 times.
✓ Branch 1 taken 5190 times.
|
22108 | for (i = 0; i < num_subfr; i++) { |
| 226 |
2/2✓ Branch 0 taken 169180 times.
✓ Branch 1 taken 16918 times.
|
186098 | for (j = 0; j < LP_FILTER_ORDER; j++) |
| 227 | 169180 | lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j]; | |
| 228 | |||
| 229 | 16918 | ff_amrwb_lsp2lpc(lsfint, Az, LP_FILTER_ORDER); | |
| 230 | 16918 | Az += LP_FILTER_ORDER; | |
| 231 | 16918 | t += t0; | |
| 232 | } | ||
| 233 | 5190 | } | |
| 234 | |||
| 235 | /** | ||
| 236 | * Evaluate the adaptive impulse response. | ||
| 237 | */ | ||
| 238 | 16918 | static void eval_ir(const float *Az, int pitch_lag, float *freq, | |
| 239 | float pitch_sharp_factor) | ||
| 240 | { | ||
| 241 | float tmp1[SUBFR_SIZE+1], tmp2[LP_FILTER_ORDER+1]; | ||
| 242 | int i; | ||
| 243 | |||
| 244 | 16918 | tmp1[0] = 1.0; | |
| 245 |
2/2✓ Branch 0 taken 169180 times.
✓ Branch 1 taken 16918 times.
|
186098 | for (i = 0; i < LP_FILTER_ORDER; i++) { |
| 246 | 169180 | tmp1[i+1] = Az[i] * ff_pow_0_55[i]; | |
| 247 | 169180 | tmp2[i ] = Az[i] * ff_pow_0_7 [i]; | |
| 248 | } | ||
| 249 | 16918 | memset(tmp1 + 11, 0, 37 * sizeof(float)); | |
| 250 | |||
| 251 | 16918 | ff_celp_lp_synthesis_filterf(freq, tmp2, tmp1, SUBFR_SIZE, | |
| 252 | LP_FILTER_ORDER); | ||
| 253 | |||
| 254 | 16918 | pitch_sharpening(pitch_lag, pitch_sharp_factor, freq); | |
| 255 | 16918 | } | |
| 256 | |||
| 257 | /** | ||
| 258 | * Evaluate the convolution of a vector with a sparse vector. | ||
| 259 | */ | ||
| 260 | 16918 | static void convolute_with_sparse(float *out, const AMRFixed *pulses, | |
| 261 | const float *shape, int length) | ||
| 262 | { | ||
| 263 | int i, j; | ||
| 264 | |||
| 265 | 16918 | memset(out, 0, length*sizeof(float)); | |
| 266 |
2/2✓ Branch 0 taken 59724 times.
✓ Branch 1 taken 16918 times.
|
76642 | for (i = 0; i < pulses->n; i++) |
| 267 |
2/2✓ Branch 0 taken 1579408 times.
✓ Branch 1 taken 59724 times.
|
1639132 | for (j = pulses->x[i]; j < length; j++) |
| 268 | 1579408 | out[j] += pulses->y[i] * shape[j - pulses->x[i]]; | |
| 269 | 16918 | } | |
| 270 | |||
| 271 | /** | ||
| 272 | * Apply postfilter, very similar to AMR one. | ||
| 273 | */ | ||
| 274 | 3370 | static void postfilter_5k0(SiprContext *ctx, const float *lpc, float *samples) | |
| 275 | { | ||
| 276 | float buf[SUBFR_SIZE + LP_FILTER_ORDER]; | ||
| 277 | 3370 | float *pole_out = buf + LP_FILTER_ORDER; | |
| 278 | float lpc_n[LP_FILTER_ORDER]; | ||
| 279 | float lpc_d[LP_FILTER_ORDER]; | ||
| 280 | int i; | ||
| 281 | |||
| 282 |
2/2✓ Branch 0 taken 33700 times.
✓ Branch 1 taken 3370 times.
|
37070 | for (i = 0; i < LP_FILTER_ORDER; i++) { |
| 283 | 33700 | lpc_d[i] = lpc[i] * ff_pow_0_75[i]; | |
| 284 | 33700 | lpc_n[i] = lpc[i] * ff_pow_0_5 [i]; | |
| 285 | }; | ||
| 286 | |||
| 287 | 3370 | memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem, | |
| 288 | LP_FILTER_ORDER*sizeof(float)); | ||
| 289 | |||
| 290 | 3370 | ff_celp_lp_synthesis_filterf(pole_out, lpc_d, samples, SUBFR_SIZE, | |
| 291 | LP_FILTER_ORDER); | ||
| 292 | |||
| 293 | 3370 | memcpy(ctx->postfilter_mem, pole_out + SUBFR_SIZE - LP_FILTER_ORDER, | |
| 294 | LP_FILTER_ORDER*sizeof(float)); | ||
| 295 | |||
| 296 | 3370 | ff_tilt_compensation(&ctx->tilt_mem, 0.4, pole_out, SUBFR_SIZE); | |
| 297 | |||
| 298 | 3370 | memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem5k0, | |
| 299 | LP_FILTER_ORDER*sizeof(*pole_out)); | ||
| 300 | |||
| 301 | 3370 | memcpy(ctx->postfilter_mem5k0, pole_out + SUBFR_SIZE - LP_FILTER_ORDER, | |
| 302 | LP_FILTER_ORDER*sizeof(*pole_out)); | ||
| 303 | |||
| 304 | 3370 | ff_celp_lp_zero_synthesis_filterf(samples, lpc_n, pole_out, SUBFR_SIZE, | |
| 305 | LP_FILTER_ORDER); | ||
| 306 | |||
| 307 | 3370 | } | |
| 308 | |||
| 309 | 16918 | static void decode_fixed_sparse(AMRFixed *fixed_sparse, const int16_t *pulses, | |
| 310 | SiprMode mode, int low_gain) | ||
| 311 | { | ||
| 312 | int i; | ||
| 313 | |||
| 314 |
3/3✓ Branch 0 taken 10086 times.
✓ Branch 1 taken 3462 times.
✓ Branch 2 taken 3370 times.
|
16918 | switch (mode) { |
| 315 | 10086 | case MODE_6k5: | |
| 316 |
2/2✓ Branch 0 taken 30258 times.
✓ Branch 1 taken 10086 times.
|
40344 | for (i = 0; i < 3; i++) { |
| 317 | 30258 | fixed_sparse->x[i] = 3 * (pulses[i] & 0xf) + i; | |
| 318 |
2/2✓ Branch 0 taken 15045 times.
✓ Branch 1 taken 15213 times.
|
30258 | fixed_sparse->y[i] = pulses[i] & 0x10 ? -1 : 1; |
| 319 | } | ||
| 320 | 10086 | fixed_sparse->n = 3; | |
| 321 | 10086 | break; | |
| 322 | 3462 | case MODE_8k5: | |
| 323 |
2/2✓ Branch 0 taken 10386 times.
✓ Branch 1 taken 3462 times.
|
13848 | for (i = 0; i < 3; i++) { |
| 324 | 10386 | fixed_sparse->x[2*i ] = 3 * ((pulses[i] >> 4) & 0xf) + i; | |
| 325 | 10386 | fixed_sparse->x[2*i + 1] = 3 * ( pulses[i] & 0xf) + i; | |
| 326 | |||
| 327 |
2/2✓ Branch 0 taken 5183 times.
✓ Branch 1 taken 5203 times.
|
10386 | fixed_sparse->y[2*i ] = (pulses[i] & 0x100) ? -1.0: 1.0; |
| 328 | |||
| 329 | 10386 | fixed_sparse->y[2*i + 1] = | |
| 330 | 10386 | (fixed_sparse->x[2*i + 1] < fixed_sparse->x[2*i]) ? | |
| 331 |
2/2✓ Branch 0 taken 5450 times.
✓ Branch 1 taken 4936 times.
|
10386 | -fixed_sparse->y[2*i ] : fixed_sparse->y[2*i]; |
| 332 | } | ||
| 333 | |||
| 334 | 3462 | fixed_sparse->n = 6; | |
| 335 | 3462 | break; | |
| 336 | 3370 | case MODE_5k0: | |
| 337 | default: | ||
| 338 |
2/2✓ Branch 0 taken 1954 times.
✓ Branch 1 taken 1416 times.
|
3370 | if (low_gain) { |
| 339 | 1954 | int offset = (pulses[0] & 0x200) ? 2 : 0; | |
| 340 | 1954 | int val = pulses[0]; | |
| 341 | |||
| 342 |
2/2✓ Branch 0 taken 5862 times.
✓ Branch 1 taken 1954 times.
|
7816 | for (i = 0; i < 3; i++) { |
| 343 | 5862 | int index = (val & 0x7) * 6 + 4 - i*2; | |
| 344 | |||
| 345 |
2/2✓ Branch 0 taken 2936 times.
✓ Branch 1 taken 2926 times.
|
5862 | fixed_sparse->y[i] = (offset + index) & 0x3 ? -1 : 1; |
| 346 | 5862 | fixed_sparse->x[i] = index; | |
| 347 | |||
| 348 | 5862 | val >>= 3; | |
| 349 | } | ||
| 350 | 1954 | fixed_sparse->n = 3; | |
| 351 | } else { | ||
| 352 | 1416 | int pulse_subset = (pulses[0] >> 8) & 1; | |
| 353 | |||
| 354 | 1416 | fixed_sparse->x[0] = ((pulses[0] >> 4) & 15) * 3 + pulse_subset; | |
| 355 | 1416 | fixed_sparse->x[1] = ( pulses[0] & 15) * 3 + pulse_subset + 1; | |
| 356 | |||
| 357 |
2/2✓ Branch 0 taken 705 times.
✓ Branch 1 taken 711 times.
|
1416 | fixed_sparse->y[0] = pulses[0] & 0x200 ? -1 : 1; |
| 358 | 1416 | fixed_sparse->y[1] = -fixed_sparse->y[0]; | |
| 359 | 1416 | fixed_sparse->n = 2; | |
| 360 | } | ||
| 361 | 3370 | break; | |
| 362 | } | ||
| 363 | 16918 | } | |
| 364 | |||
| 365 | 5190 | static void decode_frame(SiprContext *ctx, SiprParameters *params, | |
| 366 | float *out_data) | ||
| 367 | { | ||
| 368 | int i, j; | ||
| 369 | 5190 | int subframe_count = modes[ctx->mode].subframe_count; | |
| 370 | 5190 | int frame_size = subframe_count * SUBFR_SIZE; | |
| 371 | float Az[LP_FILTER_ORDER * MAX_SUBFRAME_COUNT]; | ||
| 372 | float *excitation; | ||
| 373 | float ir_buf[SUBFR_SIZE + LP_FILTER_ORDER]; | ||
| 374 | float lsf_new[LP_FILTER_ORDER]; | ||
| 375 | 5190 | float *impulse_response = ir_buf + LP_FILTER_ORDER; | |
| 376 | 5190 | float *synth = ctx->synth_buf + 16; // 16 instead of LP_FILTER_ORDER for | |
| 377 | // memory alignment | ||
| 378 | 5190 | int t0_first = 0; | |
| 379 | AMRFixed fixed_cb; | ||
| 380 | |||
| 381 | 5190 | memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float)); | |
| 382 | 5190 | lsf_decode_fp(lsf_new, ctx->lsf_history, params); | |
| 383 | |||
| 384 | 5190 | sipr_decode_lp(lsf_new, ctx->lsp_history, Az, subframe_count); | |
| 385 | |||
| 386 | 5190 | memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float)); | |
| 387 | |||
| 388 | 5190 | excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL; | |
| 389 | |||
| 390 |
2/2✓ Branch 0 taken 16918 times.
✓ Branch 1 taken 5190 times.
|
22108 | for (i = 0; i < subframe_count; i++) { |
| 391 | 16918 | float *pAz = Az + i*LP_FILTER_ORDER; | |
| 392 | float fixed_vector[SUBFR_SIZE]; | ||
| 393 | int T0,T0_frac; | ||
| 394 | float pitch_gain, gain_code, avg_energy; | ||
| 395 | |||
| 396 | 16918 | ff_decode_pitch_lag(&T0, &T0_frac, params->pitch_delay[i], t0_first, i, | |
| 397 | 16918 | ctx->mode == MODE_5k0, 6); | |
| 398 | |||
| 399 |
6/6✓ Branch 0 taken 11728 times.
✓ Branch 1 taken 5190 times.
✓ Branch 2 taken 5190 times.
✓ Branch 3 taken 6538 times.
✓ Branch 4 taken 674 times.
✓ Branch 5 taken 4516 times.
|
16918 | if (i == 0 || (i == 2 && ctx->mode == MODE_5k0)) |
| 400 | 5864 | t0_first = T0; | |
| 401 | |||
| 402 | 16918 | ff_acelp_interpolatef(excitation, excitation - T0 + (T0_frac <= 0), | |
| 403 | ff_b60_sinc, 6, | ||
| 404 |
2/2✓ Branch 0 taken 12303 times.
✓ Branch 1 taken 4615 times.
|
16918 | 2 * ((2 + T0_frac)%3 + 1), LP_FILTER_ORDER, |
| 405 | SUBFR_SIZE); | ||
| 406 | |||
| 407 | 16918 | decode_fixed_sparse(&fixed_cb, params->fc_indexes[i], ctx->mode, | |
| 408 | 16918 | ctx->past_pitch_gain < 0.8); | |
| 409 | |||
| 410 | 16918 | eval_ir(pAz, T0, impulse_response, modes[ctx->mode].pitch_sharp_factor); | |
| 411 | |||
| 412 | 16918 | convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response, | |
| 413 | SUBFR_SIZE); | ||
| 414 | |||
| 415 | 16918 | avg_energy = (0.01 + ff_scalarproduct_float_c(fixed_vector, | |
| 416 | fixed_vector, | ||
| 417 | 16918 | SUBFR_SIZE)) / | |
| 418 | SUBFR_SIZE; | ||
| 419 | |||
| 420 | 16918 | ctx->past_pitch_gain = pitch_gain = gain_cb[params->gc_index[i]][0]; | |
| 421 | |||
| 422 | 16918 | gain_code = ff_amr_set_fixed_gain(gain_cb[params->gc_index[i]][1], | |
| 423 | 16918 | avg_energy, ctx->energy_history, | |
| 424 | 34 - 15.0/(0.05*M_LN10/M_LN2), | ||
| 425 | pred); | ||
| 426 | |||
| 427 | 16918 | ff_weighted_vector_sumf(excitation, excitation, fixed_vector, | |
| 428 | pitch_gain, gain_code, SUBFR_SIZE); | ||
| 429 | |||
| 430 | 16918 | pitch_gain *= 0.5 * pitch_gain; | |
| 431 |
2/2✓ Branch 0 taken 4278 times.
✓ Branch 1 taken 12640 times.
|
16918 | pitch_gain = FFMIN(pitch_gain, 0.4); |
| 432 | |||
| 433 | 16918 | ctx->gain_mem = 0.7 * ctx->gain_mem + 0.3 * pitch_gain; | |
| 434 |
2/2✓ Branch 0 taken 5239 times.
✓ Branch 1 taken 11679 times.
|
16918 | ctx->gain_mem = FFMIN(ctx->gain_mem, pitch_gain); |
| 435 | 16918 | gain_code *= ctx->gain_mem; | |
| 436 | |||
| 437 |
2/2✓ Branch 0 taken 812064 times.
✓ Branch 1 taken 16918 times.
|
828982 | for (j = 0; j < SUBFR_SIZE; j++) |
| 438 | 812064 | fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j]; | |
| 439 | |||
| 440 |
2/2✓ Branch 0 taken 3370 times.
✓ Branch 1 taken 13548 times.
|
16918 | if (ctx->mode == MODE_5k0) { |
| 441 | 3370 | postfilter_5k0(ctx, pAz, fixed_vector); | |
| 442 | |||
| 443 | 3370 | ff_celp_lp_synthesis_filterf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE, | |
| 444 | pAz, excitation, SUBFR_SIZE, | ||
| 445 | LP_FILTER_ORDER); | ||
| 446 | } | ||
| 447 | |||
| 448 | 16918 | ff_celp_lp_synthesis_filterf(synth + i*SUBFR_SIZE, pAz, fixed_vector, | |
| 449 | SUBFR_SIZE, LP_FILTER_ORDER); | ||
| 450 | |||
| 451 | 16918 | excitation += SUBFR_SIZE; | |
| 452 | } | ||
| 453 | |||
| 454 | 5190 | memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER, | |
| 455 | LP_FILTER_ORDER * sizeof(float)); | ||
| 456 | |||
| 457 |
2/2✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4516 times.
|
5190 | if (ctx->mode == MODE_5k0) { |
| 458 |
2/2✓ Branch 0 taken 3370 times.
✓ Branch 1 taken 674 times.
|
4044 | for (i = 0; i < subframe_count; i++) { |
| 459 | 3370 | float energy = ff_scalarproduct_float_c(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i * SUBFR_SIZE, | |
| 460 | 3370 | ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i * SUBFR_SIZE, | |
| 461 | SUBFR_SIZE); | ||
| 462 | 3370 | ff_adaptive_gain_control(&synth[i * SUBFR_SIZE], | |
| 463 | 3370 | &synth[i * SUBFR_SIZE], energy, | |
| 464 | SUBFR_SIZE, 0.9, &ctx->postfilter_agc); | ||
| 465 | } | ||
| 466 | |||
| 467 | 674 | memcpy(ctx->postfilter_syn5k0, ctx->postfilter_syn5k0 + frame_size, | |
| 468 | LP_FILTER_ORDER*sizeof(float)); | ||
| 469 | } | ||
| 470 | 5190 | memmove(ctx->excitation, excitation - PITCH_DELAY_MAX - L_INTERPOL, | |
| 471 | (PITCH_DELAY_MAX + L_INTERPOL) * sizeof(float)); | ||
| 472 | |||
| 473 | 5190 | ff_acelp_apply_order_2_transfer_function(out_data, synth, | |
| 474 | 5190 | (const float[2]) {-1.99997 , 1.000000000}, | |
| 475 | 5190 | (const float[2]) {-1.93307352, 0.935891986}, | |
| 476 | 0.939805806, | ||
| 477 | 5190 | ctx->highpass_filt_mem, | |
| 478 | frame_size); | ||
| 479 | 5190 | } | |
| 480 | |||
| 481 | 9 | static av_cold int sipr_decoder_init(AVCodecContext * avctx) | |
| 482 | { | ||
| 483 | 9 | SiprContext *ctx = avctx->priv_data; | |
| 484 | int i; | ||
| 485 | |||
| 486 |
4/5✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
9 | switch (avctx->block_align) { |
| 487 | 2 | case 20: ctx->mode = MODE_16k; break; | |
| 488 | 3 | case 19: ctx->mode = MODE_8k5; break; | |
| 489 | 2 | case 29: ctx->mode = MODE_6k5; break; | |
| 490 | 2 | case 37: ctx->mode = MODE_5k0; break; | |
| 491 | ✗ | default: | |
| 492 | ✗ | if (avctx->bit_rate > 12200) ctx->mode = MODE_16k; | |
| 493 | ✗ | else if (avctx->bit_rate > 7500 ) ctx->mode = MODE_8k5; | |
| 494 | ✗ | else if (avctx->bit_rate > 5750 ) ctx->mode = MODE_6k5; | |
| 495 | ✗ | else ctx->mode = MODE_5k0; | |
| 496 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 497 | "Invalid block_align: %d. Mode %s guessed based on bitrate: %"PRId64"\n", | ||
| 498 | ✗ | avctx->block_align, modes[ctx->mode].mode_name, avctx->bit_rate); | |
| 499 | } | ||
| 500 | |||
| 501 | 9 | av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", modes[ctx->mode].mode_name); | |
| 502 | |||
| 503 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
|
9 | if (ctx->mode == MODE_16k) { |
| 504 | 2 | ff_sipr_init_16k(ctx); | |
| 505 | 2 | ctx->decode_frame = ff_sipr_decode_frame_16k; | |
| 506 | } else { | ||
| 507 | 7 | ctx->decode_frame = decode_frame; | |
| 508 | } | ||
| 509 | |||
| 510 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 9 times.
|
99 | for (i = 0; i < LP_FILTER_ORDER; i++) |
| 511 | 90 | ctx->lsp_history[i] = cos((i+1) * M_PI / (LP_FILTER_ORDER + 1)); | |
| 512 | |||
| 513 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 9 times.
|
45 | for (i = 0; i < 4; i++) |
| 514 | 36 | ctx->energy_history[i] = -14; | |
| 515 | |||
| 516 | 9 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 517 | 9 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; | |
| 518 | 9 | avctx->sample_fmt = AV_SAMPLE_FMT_FLT; | |
| 519 | |||
| 520 | 9 | return 0; | |
| 521 | } | ||
| 522 | |||
| 523 | 6427 | static int sipr_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 524 | int *got_frame_ptr, AVPacket *avpkt) | ||
| 525 | { | ||
| 526 | 6427 | SiprContext *ctx = avctx->priv_data; | |
| 527 | 6427 | const uint8_t *buf=avpkt->data; | |
| 528 | SiprParameters parm; | ||
| 529 | 6427 | const SiprModeParam *mode_par = &modes[ctx->mode]; | |
| 530 | GetBitContext gb; | ||
| 531 | float *samples; | ||
| 532 |
2/2✓ Branch 0 taken 3255 times.
✓ Branch 1 taken 3172 times.
|
6427 | int subframe_size = ctx->mode == MODE_16k ? L_SUBFR_16k : SUBFR_SIZE; |
| 533 | int i, ret; | ||
| 534 | |||
| 535 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6427 times.
|
6427 | if (avpkt->size < (mode_par->bits_per_frame >> 3)) { |
| 536 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 537 | "Error processing packet: packet size (%d) too small\n", | ||
| 538 | avpkt->size); | ||
| 539 | ✗ | return AVERROR_INVALIDDATA; | |
| 540 | } | ||
| 541 | |||
| 542 | /* get output buffer */ | ||
| 543 | 6427 | frame->nb_samples = mode_par->frames_per_packet * subframe_size * | |
| 544 | 6427 | mode_par->subframe_count; | |
| 545 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6427 times.
|
6427 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 546 | ✗ | return ret; | |
| 547 | 6427 | samples = (float *)frame->data[0]; | |
| 548 | |||
| 549 | 6427 | init_get_bits(&gb, buf, mode_par->bits_per_frame); | |
| 550 | |||
| 551 |
2/2✓ Branch 0 taken 8445 times.
✓ Branch 1 taken 6427 times.
|
14872 | for (i = 0; i < mode_par->frames_per_packet; i++) { |
| 552 | 8445 | decode_parameters(&parm, &gb, mode_par); | |
| 553 | |||
| 554 | 8445 | ctx->decode_frame(ctx, &parm, samples); | |
| 555 | |||
| 556 | 8445 | samples += subframe_size * mode_par->subframe_count; | |
| 557 | } | ||
| 558 | |||
| 559 | 6427 | *got_frame_ptr = 1; | |
| 560 | |||
| 561 | 6427 | return mode_par->bits_per_frame >> 3; | |
| 562 | } | ||
| 563 | |||
| 564 | const FFCodec ff_sipr_decoder = { | ||
| 565 | .p.name = "sipr", | ||
| 566 | CODEC_LONG_NAME("RealAudio SIPR / ACELP.NET"), | ||
| 567 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 568 | .p.id = AV_CODEC_ID_SIPR, | ||
| 569 | .priv_data_size = sizeof(SiprContext), | ||
| 570 | .init = sipr_decoder_init, | ||
| 571 | FF_CODEC_DECODE_CB(sipr_decode_frame), | ||
| 572 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF, | ||
| 573 | }; | ||
| 574 |