Mercurial > libavcodec.hg
annotate sipr.c @ 10889:de32bff741ea libavcodec
Split some SIPR structs to a header file for the upcoming SIPR16k commit
| author | vitor |
|---|---|
| date | Sat, 16 Jan 2010 03:40:25 +0000 |
| parents | fb42dfc877cc |
| children | 36587d8c1201 |
| rev | line source |
|---|---|
| 10836 | 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 | |
| 10873 | 27 #include "libavutil/mathematics.h" |
| 10836 | 28 #include "avcodec.h" |
| 29 #define ALT_BITSTREAM_READER_LE | |
| 30 #include "get_bits.h" | |
| 31 #include "dsputil.h" | |
| 32 | |
| 33 #include "lsp.h" | |
| 34 #include "celp_math.h" | |
| 35 #include "acelp_vectors.h" | |
| 36 #include "acelp_pitch_delay.h" | |
| 37 #include "acelp_filters.h" | |
| 38 #include "celp_filters.h" | |
| 39 | |
| 10871 | 40 #define MAX_SUBFRAME_COUNT 5 |
| 41 | |
|
10889
de32bff741ea
Split some SIPR structs to a header file for the upcoming SIPR16k commit
vitor
parents:
10873
diff
changeset
|
42 #include "sipr.h" |
| 10836 | 43 #include "siprdata.h" |
| 44 | |
| 45 typedef struct { | |
| 46 const char *mode_name; | |
| 47 uint16_t bits_per_frame; | |
| 48 uint8_t subframe_count; | |
| 49 uint8_t frames_per_packet; | |
| 50 float pitch_sharp_factor; | |
| 51 | |
| 52 /* bitstream parameters */ | |
| 53 uint8_t number_of_fc_indexes; | |
| 54 | |
| 55 /** size in bits of the i-th stage vector of quantizer */ | |
| 56 uint8_t vq_indexes_bits[5]; | |
| 57 | |
| 58 /** size in bits of the adaptive-codebook index for every subframe */ | |
| 59 uint8_t pitch_delay_bits[5]; | |
| 60 | |
| 61 uint8_t gp_index_bits; | |
| 62 uint8_t fc_index_bits[10]; ///< size in bits of the fixed codebook indexes | |
| 63 uint8_t gc_index_bits; ///< size in bits of the gain codebook indexes | |
| 64 } SiprModeParam; | |
| 65 | |
| 66 static const SiprModeParam modes[MODE_COUNT] = { | |
| 67 [MODE_8k5] = { | |
| 68 .mode_name = "8k5", | |
| 69 .bits_per_frame = 152, | |
| 70 .subframe_count = 3, | |
| 71 .frames_per_packet = 1, | |
| 72 .pitch_sharp_factor = 0.8, | |
| 73 | |
| 74 .number_of_fc_indexes = 3, | |
| 75 .vq_indexes_bits = {6, 7, 7, 7, 5}, | |
| 76 .pitch_delay_bits = {8, 5, 5}, | |
| 77 .gp_index_bits = 0, | |
| 78 .fc_index_bits = {9, 9, 9}, | |
| 79 .gc_index_bits = 7 | |
| 80 }, | |
| 81 | |
| 82 [MODE_6k5] = { | |
| 83 .mode_name = "6k5", | |
| 84 .bits_per_frame = 232, | |
| 85 .subframe_count = 3, | |
| 86 .frames_per_packet = 2, | |
| 87 .pitch_sharp_factor = 0.8, | |
| 88 | |
| 89 .number_of_fc_indexes = 3, | |
| 90 .vq_indexes_bits = {6, 7, 7, 7, 5}, | |
| 91 .pitch_delay_bits = {8, 5, 5}, | |
| 92 .gp_index_bits = 0, | |
| 93 .fc_index_bits = {5, 5, 5}, | |
| 94 .gc_index_bits = 7 | |
| 95 }, | |
| 96 | |
| 97 [MODE_5k0] = { | |
| 98 .mode_name = "5k0", | |
| 99 .bits_per_frame = 296, | |
| 100 .subframe_count = 5, | |
| 101 .frames_per_packet = 2, | |
| 102 .pitch_sharp_factor = 0.85, | |
| 103 | |
| 104 .number_of_fc_indexes = 1, | |
| 105 .vq_indexes_bits = {6, 7, 7, 7, 5}, | |
| 106 .pitch_delay_bits = {8, 5, 8, 5, 5}, | |
| 107 .gp_index_bits = 0, | |
| 108 .fc_index_bits = {10}, | |
| 109 .gc_index_bits = 7 | |
| 110 } | |
| 111 }; | |
| 112 | |
| 113 static void dequant(float *out, const int *idx, const float *cbs[]) | |
| 114 { | |
| 115 int i; | |
| 116 int stride = 2; | |
| 117 int num_vec = 5; | |
| 118 | |
| 119 for (i = 0; i < num_vec; i++) | |
| 120 memcpy(out + stride*i, cbs[i] + stride*idx[i], stride*sizeof(float)); | |
| 121 | |
| 122 } | |
| 123 | |
| 124 static void lsf_decode_fp(float *lsfnew, float *lsf_history, | |
| 125 const SiprParameters *parm) | |
| 126 { | |
| 127 int i; | |
| 128 float lsf_tmp[LP_FILTER_ORDER]; | |
| 129 | |
| 130 dequant(lsf_tmp, parm->vq_indexes, lsf_codebooks); | |
| 131 | |
| 132 for (i = 0; i < LP_FILTER_ORDER; i++) | |
| 133 lsfnew[i] = lsf_history[i] * 0.33 + lsf_tmp[i] + mean_lsf[i]; | |
| 134 | |
| 135 ff_sort_nearly_sorted_floats(lsfnew, LP_FILTER_ORDER - 1); | |
| 136 | |
| 137 /* Note that a minimum distance is not enforced between the last value and | |
| 138 the previous one, contrary to what is done in ff_acelp_reorder_lsf() */ | |
| 139 ff_set_min_dist_lsf(lsfnew, LSFQ_DIFF_MIN, LP_FILTER_ORDER - 1); | |
| 140 lsfnew[9] = FFMIN(lsfnew[LP_FILTER_ORDER - 1], 1.3 * M_PI); | |
| 141 | |
| 142 memcpy(lsf_history, lsf_tmp, LP_FILTER_ORDER * sizeof(*lsf_history)); | |
| 143 | |
| 144 for (i = 0; i < LP_FILTER_ORDER - 1; i++) | |
| 145 lsfnew[i] = cos(lsfnew[i]); | |
| 146 lsfnew[LP_FILTER_ORDER - 1] *= 6.153848 / M_PI; | |
| 147 } | |
| 148 | |
| 149 /** Apply pitch lag to the fixed vector (AMR section 6.1.2). */ | |
| 150 static void pitch_sharpening(int pitch_lag_int, float beta, | |
| 151 float *fixed_vector) | |
| 152 { | |
| 153 int i; | |
| 154 | |
| 155 for (i = pitch_lag_int; i < SUBFR_SIZE; i++) | |
| 156 fixed_vector[i] += beta * fixed_vector[i - pitch_lag_int]; | |
| 157 } | |
| 158 | |
| 159 /** | |
| 160 * Extracts decoding parameters from the input bitstream. | |
| 161 * @param parms parameters structure | |
| 162 * @param pgb pointer to initialized GetBitContext structure | |
| 163 */ | |
| 164 static void decode_parameters(SiprParameters* parms, GetBitContext *pgb, | |
| 165 const SiprModeParam *p) | |
| 166 { | |
| 167 int i, j; | |
| 168 | |
| 169 for (i = 0; i < 5; i++) | |
| 170 parms->vq_indexes[i] = get_bits(pgb, p->vq_indexes_bits[i]); | |
| 171 | |
| 172 for (i = 0; i < p->subframe_count; i++) { | |
| 173 parms->pitch_delay[i] = get_bits(pgb, p->pitch_delay_bits[i]); | |
| 174 parms->gp_index[i] = get_bits(pgb, p->gp_index_bits); | |
| 175 | |
| 176 for (j = 0; j < p->number_of_fc_indexes; j++) | |
| 177 parms->fc_indexes[i][j] = get_bits(pgb, p->fc_index_bits[j]); | |
| 178 | |
| 179 parms->gc_index[i] = get_bits(pgb, p->gc_index_bits); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 static void lsp2lpc_sipr(const double *lsp, float *Az) | |
| 184 { | |
| 185 int lp_half_order = LP_FILTER_ORDER >> 1; | |
| 10871 | 186 double buf[(LP_FILTER_ORDER >> 1) + 1]; |
| 187 double pa[(LP_FILTER_ORDER >> 1) + 1]; | |
| 10836 | 188 double *qa = buf + 1; |
| 189 int i,j; | |
| 190 | |
| 191 qa[-1] = 0.0; | |
| 192 | |
| 193 ff_lsp2polyf(lsp , pa, lp_half_order ); | |
| 194 ff_lsp2polyf(lsp + 1, qa, lp_half_order - 1); | |
| 195 | |
| 196 for (i = 1, j = LP_FILTER_ORDER - 1; i < lp_half_order; i++, j--) { | |
| 197 double paf = pa[i] * (1 + lsp[LP_FILTER_ORDER - 1]); | |
| 198 double qaf = (qa[i] - qa[i-2]) * (1 - lsp[LP_FILTER_ORDER - 1]); | |
| 199 Az[i-1] = (paf + qaf) * 0.5; | |
| 200 Az[j-1] = (paf - qaf) * 0.5; | |
| 201 } | |
| 202 | |
| 203 Az[lp_half_order - 1] = (1.0 + lsp[LP_FILTER_ORDER - 1]) * | |
| 204 pa[lp_half_order] * 0.5; | |
| 205 | |
| 206 Az[LP_FILTER_ORDER - 1] = lsp[LP_FILTER_ORDER - 1]; | |
| 207 } | |
| 208 | |
| 209 static void sipr_decode_lp(float *lsfnew, const float *lsfold, float *Az, | |
| 210 int num_subfr) | |
| 211 { | |
| 212 double lsfint[LP_FILTER_ORDER]; | |
| 213 int i,j; | |
| 214 float t, t0 = 1.0 / num_subfr; | |
| 215 | |
| 216 t = t0 * 0.5; | |
| 217 for (i = 0; i < num_subfr; i++) { | |
| 218 for (j = 0; j < LP_FILTER_ORDER; j++) | |
| 219 lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j]; | |
| 220 | |
| 221 lsp2lpc_sipr(lsfint, Az); | |
| 222 Az += LP_FILTER_ORDER; | |
| 223 t += t0; | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 /** | |
| 228 * Evaluates the adaptative impulse response. | |
| 229 */ | |
| 230 static void eval_ir(const float *Az, int pitch_lag, float *freq, | |
| 231 float pitch_sharp_factor) | |
| 232 { | |
| 233 float tmp1[SUBFR_SIZE+1], tmp2[LP_FILTER_ORDER+1]; | |
| 234 int i; | |
| 235 | |
| 236 tmp1[0] = 1.; | |
| 237 for (i = 0; i < LP_FILTER_ORDER; i++) { | |
| 238 tmp1[i+1] = Az[i] * ff_pow_0_55[i]; | |
| 239 tmp2[i ] = Az[i] * ff_pow_0_7 [i]; | |
| 240 } | |
| 241 memset(tmp1 + 11, 0, 37 * sizeof(float)); | |
| 242 | |
| 243 ff_celp_lp_synthesis_filterf(freq, tmp2, tmp1, SUBFR_SIZE, | |
| 244 LP_FILTER_ORDER); | |
| 245 | |
| 246 pitch_sharpening(pitch_lag, pitch_sharp_factor, freq); | |
| 247 } | |
| 248 | |
| 249 /** | |
| 250 * Evaluates the convolution of a vector with a sparse vector. | |
| 251 */ | |
| 252 static void convolute_with_sparse(float *out, const AMRFixed *pulses, | |
| 253 const float *shape, int length) | |
| 254 { | |
| 255 int i, j; | |
| 256 | |
| 257 memset(out, 0, length*sizeof(float)); | |
| 258 for (i = 0; i < pulses->n; i++) | |
| 259 for (j = pulses->x[i]; j < length; j++) | |
| 260 out[j] += pulses->y[i] * shape[j - pulses->x[i]]; | |
| 261 } | |
| 262 | |
| 263 /** | |
| 264 * Apply postfilter, very similar to AMR one. | |
| 265 */ | |
| 266 static void postfilter_5k0(SiprContext *ctx, const float *lpc, float *samples) | |
| 267 { | |
| 268 float buf[SUBFR_SIZE + LP_FILTER_ORDER]; | |
| 269 float *pole_out = buf + LP_FILTER_ORDER; | |
| 270 float lpc_n[LP_FILTER_ORDER]; | |
| 271 float lpc_d[LP_FILTER_ORDER]; | |
| 272 int i; | |
| 273 | |
| 274 for (i = 0; i < LP_FILTER_ORDER; i++) { | |
| 275 lpc_d[i] = lpc[i] * ff_pow_0_75[i]; | |
| 276 lpc_n[i] = lpc[i] * pow_0_5 [i]; | |
| 277 }; | |
| 278 | |
| 279 memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem, | |
| 280 LP_FILTER_ORDER*sizeof(float)); | |
| 281 | |
| 282 ff_celp_lp_synthesis_filterf(pole_out, lpc_d, samples, SUBFR_SIZE, | |
| 283 LP_FILTER_ORDER); | |
| 284 | |
| 285 memcpy(ctx->postfilter_mem, pole_out + SUBFR_SIZE - LP_FILTER_ORDER, | |
| 286 LP_FILTER_ORDER*sizeof(float)); | |
| 287 | |
| 288 ff_tilt_compensation(&ctx->tilt_mem, 0.4, pole_out, SUBFR_SIZE); | |
| 289 | |
| 290 memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem5k0, | |
| 291 LP_FILTER_ORDER*sizeof(*pole_out)); | |
| 292 | |
| 293 memcpy(ctx->postfilter_mem5k0, pole_out + SUBFR_SIZE - LP_FILTER_ORDER, | |
| 294 LP_FILTER_ORDER*sizeof(*pole_out)); | |
| 295 | |
| 296 ff_celp_lp_zero_synthesis_filterf(samples, lpc_n, pole_out, SUBFR_SIZE, | |
| 297 LP_FILTER_ORDER); | |
| 298 | |
| 299 } | |
| 300 | |
| 301 static void decode_fixed_sparse(AMRFixed *fixed_sparse, const int16_t *pulses, | |
| 302 SiprMode mode, int low_gain) | |
| 303 { | |
| 304 int i; | |
| 305 | |
| 306 switch (mode) { | |
| 307 case MODE_6k5: | |
| 308 for (i = 0; i < 3; i++) { | |
| 309 fixed_sparse->x[i] = 3 * (pulses[i] & 0xf) + i; | |
| 310 fixed_sparse->y[i] = pulses[i] & 0x10 ? -1 : 1; | |
| 311 } | |
| 312 fixed_sparse->n = 3; | |
| 313 break; | |
| 314 case MODE_8k5: | |
| 315 for (i = 0; i < 3; i++) { | |
| 316 fixed_sparse->x[2*i ] = 3 * ((pulses[i] >> 4) & 0xf) + i; | |
| 317 fixed_sparse->x[2*i + 1] = 3 * ( pulses[i] & 0xf) + i; | |
| 318 | |
| 319 fixed_sparse->y[2*i ] = (pulses[i] & 0x100) ? -1.0: 1.0; | |
| 320 | |
| 321 fixed_sparse->y[2*i + 1] = | |
| 322 (fixed_sparse->x[2*i + 1] < fixed_sparse->x[2*i]) ? | |
| 323 -fixed_sparse->y[2*i ] : fixed_sparse->y[2*i]; | |
| 324 } | |
| 325 | |
| 326 fixed_sparse->n = 6; | |
| 327 break; | |
| 328 case MODE_5k0: | |
| 329 default: | |
| 330 if (low_gain) { | |
| 331 int offset = (pulses[0] & 0x200) ? 2 : 0; | |
| 332 int val = pulses[0]; | |
| 333 | |
| 334 for (i = 0; i < 3; i++) { | |
| 335 int index = (val & 0x7) * 6 + 4 - i*2; | |
| 336 | |
| 337 fixed_sparse->y[i] = (offset + index) & 0x3 ? -1 : 1; | |
| 338 fixed_sparse->x[i] = index; | |
| 339 | |
| 340 val >>= 3; | |
| 341 } | |
| 342 fixed_sparse->n = 3; | |
| 343 } else { | |
| 344 int pulse_subset = (pulses[0] >> 8) & 1; | |
| 345 | |
| 346 fixed_sparse->x[0] = ((pulses[0] >> 4) & 15) * 3 + pulse_subset; | |
| 347 fixed_sparse->x[1] = ( pulses[0] & 15) * 3 + pulse_subset + 1; | |
| 348 | |
| 349 fixed_sparse->y[0] = pulses[0] & 0x200 ? -1 : 1; | |
| 350 fixed_sparse->y[1] = -fixed_sparse->y[0]; | |
| 351 fixed_sparse->n = 2; | |
| 352 } | |
| 353 break; | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 static void decode_frame(SiprContext *ctx, SiprParameters *params, | |
| 358 float *out_data) | |
| 359 { | |
| 360 int i, j; | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
361 int subframe_count = modes[ctx->mode].subframe_count; |
|
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
362 int frame_size = subframe_count * SUBFR_SIZE; |
| 10871 | 363 float Az[LP_FILTER_ORDER * MAX_SUBFRAME_COUNT]; |
| 10836 | 364 float *excitation; |
| 365 float ir_buf[SUBFR_SIZE + LP_FILTER_ORDER]; | |
| 366 float lsf_new[LP_FILTER_ORDER]; | |
| 367 float *impulse_response = ir_buf + LP_FILTER_ORDER; | |
| 368 float *synth = ctx->synth_buf + 16; // 16 instead of LP_FILTER_ORDER for | |
| 369 // memory alignment | |
| 370 int t0_first = 0; | |
| 371 AMRFixed fixed_cb; | |
| 372 | |
| 373 memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float)); | |
| 374 lsf_decode_fp(lsf_new, ctx->lsf_history, params); | |
| 375 | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
376 sipr_decode_lp(lsf_new, ctx->lsp_history, Az, subframe_count); |
| 10836 | 377 |
| 378 memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float)); | |
| 379 | |
| 380 excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL; | |
| 381 | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
382 for (i = 0; i < subframe_count; i++) { |
| 10836 | 383 float *pAz = Az + i*LP_FILTER_ORDER; |
| 384 float fixed_vector[SUBFR_SIZE]; | |
| 385 int T0,T0_frac; | |
| 386 float pitch_gain, gain_code, avg_energy; | |
| 387 | |
| 388 ff_decode_pitch_lag(&T0, &T0_frac, params->pitch_delay[i], t0_first, i, | |
| 389 ctx->mode == MODE_5k0, 6); | |
| 390 | |
| 391 if (i == 0 || (i == 2 && ctx->mode == MODE_5k0)) | |
| 392 t0_first = T0; | |
| 393 | |
| 394 ff_acelp_interpolatef(excitation, excitation - T0 + (T0_frac <= 0), | |
| 395 ff_b60_sinc, 6, | |
| 396 2 * ((2 + T0_frac)%3 + 1), LP_FILTER_ORDER, | |
| 397 SUBFR_SIZE); | |
| 398 | |
| 399 decode_fixed_sparse(&fixed_cb, params->fc_indexes[i], ctx->mode, | |
| 400 ctx->past_pitch_gain < 0.8); | |
| 401 | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
402 eval_ir(pAz, T0, impulse_response, modes[ctx->mode].pitch_sharp_factor); |
| 10836 | 403 |
| 404 convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response, | |
| 405 SUBFR_SIZE); | |
| 406 | |
| 407 avg_energy = | |
| 408 (0.01 + ff_dot_productf(fixed_vector, fixed_vector, SUBFR_SIZE))/ | |
| 409 SUBFR_SIZE; | |
| 410 | |
| 411 ctx->past_pitch_gain = pitch_gain = gain_cb[params->gc_index[i]][0]; | |
| 412 | |
| 413 gain_code = ff_amr_set_fixed_gain(gain_cb[params->gc_index[i]][1], | |
| 414 avg_energy, ctx->energy_history, | |
| 10873 | 415 34 - 15.0/(0.05*M_LN10/M_LN2), |
| 10836 | 416 pred); |
| 417 | |
| 418 ff_weighted_vector_sumf(excitation, excitation, fixed_vector, | |
| 419 pitch_gain, gain_code, SUBFR_SIZE); | |
| 420 | |
| 421 pitch_gain *= 0.5 * pitch_gain; | |
| 422 pitch_gain = FFMIN(pitch_gain, 0.4); | |
| 423 | |
| 424 ctx->gain_mem = 0.7 * ctx->gain_mem + 0.3 * pitch_gain; | |
| 425 ctx->gain_mem = FFMIN(ctx->gain_mem, pitch_gain); | |
| 426 gain_code *= ctx->gain_mem; | |
| 427 | |
| 428 for (j = 0; j < SUBFR_SIZE; j++) | |
| 429 fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j]; | |
| 430 | |
| 431 if (ctx->mode == MODE_5k0) { | |
| 432 postfilter_5k0(ctx, pAz, fixed_vector); | |
| 433 | |
| 434 ff_celp_lp_synthesis_filterf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE, | |
| 435 pAz, excitation, SUBFR_SIZE, | |
| 436 LP_FILTER_ORDER); | |
| 437 } | |
| 438 | |
| 439 ff_celp_lp_synthesis_filterf(synth + i*SUBFR_SIZE, pAz, fixed_vector, | |
| 440 SUBFR_SIZE, LP_FILTER_ORDER); | |
| 441 | |
| 442 excitation += SUBFR_SIZE; | |
| 443 } | |
| 444 | |
| 445 memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER, | |
| 446 LP_FILTER_ORDER * sizeof(float)); | |
| 447 | |
| 448 if (ctx->mode == MODE_5k0) { | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
449 for (i = 0; i < subframe_count; i++) { |
| 10836 | 450 float energy = ff_dot_productf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE, |
| 451 ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE, | |
| 452 SUBFR_SIZE); | |
| 453 ff_adaptative_gain_control(&synth[i * SUBFR_SIZE], energy, | |
| 454 SUBFR_SIZE, 0.9, &ctx->postfilter_agc); | |
| 455 } | |
| 456 | |
| 457 memcpy(ctx->postfilter_syn5k0, ctx->postfilter_syn5k0 + frame_size, | |
| 458 LP_FILTER_ORDER*sizeof(float)); | |
| 459 } | |
| 460 memcpy(ctx->excitation, excitation - PITCH_DELAY_MAX - L_INTERPOL, | |
| 461 (PITCH_DELAY_MAX + L_INTERPOL) * sizeof(float)); | |
| 462 | |
| 463 ff_acelp_apply_order_2_transfer_function(synth, | |
| 464 (const float[2]) {-1.99997 , 1.000000000}, | |
| 465 (const float[2]) {-1.93307352, 0.935891986}, | |
| 466 0.939805806, | |
| 467 ctx->highpass_filt_mem, | |
| 468 frame_size); | |
| 469 | |
| 470 ctx->dsp.vector_clipf(out_data, synth, -1, 32767./(1<<15), frame_size); | |
| 471 | |
| 472 } | |
| 473 | |
| 474 static av_cold int sipr_decoder_init(AVCodecContext * avctx) | |
| 475 { | |
| 476 SiprContext *ctx = avctx->priv_data; | |
| 477 int i; | |
| 478 | |
| 479 if (avctx->bit_rate > 12200) ctx->mode = MODE_16k; | |
| 480 else if (avctx->bit_rate > 7500 ) ctx->mode = MODE_8k5; | |
| 481 else if (avctx->bit_rate > 5750 ) ctx->mode = MODE_6k5; | |
| 482 else ctx->mode = MODE_5k0; | |
| 483 | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
484 av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", modes[ctx->mode].mode_name); |
| 10836 | 485 |
| 486 for (i = 0; i < LP_FILTER_ORDER; i++) | |
| 487 ctx->lsp_history[i] = cos((i+1) * M_PI / (LP_FILTER_ORDER + 1)); | |
| 488 | |
| 489 for (i = 0; i < 4; i++) | |
| 490 ctx->energy_history[i] = -14; | |
| 491 | |
| 492 avctx->sample_fmt = SAMPLE_FMT_FLT; | |
| 493 | |
| 494 if (ctx->mode == MODE_16k) { | |
| 495 av_log(avctx, AV_LOG_ERROR, "decoding 16kbps SIPR files is not " | |
| 496 "supported yet.\n"); | |
| 497 return -1; | |
| 498 } | |
| 499 | |
| 500 dsputil_init(&ctx->dsp, avctx); | |
| 501 | |
| 502 return 0; | |
| 503 } | |
| 504 | |
| 505 static int sipr_decode_frame(AVCodecContext *avctx, void *datap, | |
| 506 int *data_size, AVPacket *avpkt) | |
| 507 { | |
| 508 SiprContext *ctx = avctx->priv_data; | |
| 509 const uint8_t *buf=avpkt->data; | |
| 510 SiprParameters parm; | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
511 const SiprModeParam *mode_par = &modes[ctx->mode]; |
| 10836 | 512 GetBitContext gb; |
| 513 float *data = datap; | |
| 514 int i; | |
| 515 | |
| 516 ctx->avctx = avctx; | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
517 if (avpkt->size < (mode_par->bits_per_frame >> 3)) { |
| 10836 | 518 av_log(avctx, AV_LOG_ERROR, |
| 519 "Error processing packet: packet size (%d) too small\n", | |
| 520 avpkt->size); | |
| 521 | |
| 522 *data_size = 0; | |
| 523 return -1; | |
| 524 } | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
525 if (*data_size < SUBFR_SIZE * mode_par->subframe_count * sizeof(float)) { |
| 10836 | 526 av_log(avctx, AV_LOG_ERROR, |
| 527 "Error processing packet: output buffer (%d) too small\n", | |
| 528 *data_size); | |
| 529 | |
| 530 *data_size = 0; | |
| 531 return -1; | |
| 532 } | |
| 533 | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
534 init_get_bits(&gb, buf, mode_par->bits_per_frame); |
| 10836 | 535 |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
536 for (i = 0; i < mode_par->frames_per_packet; i++) { |
|
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
537 decode_parameters(&parm, &gb, mode_par); |
| 10836 | 538 decode_frame(ctx, &parm, data); |
| 539 | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
540 data += SUBFR_SIZE * mode_par->subframe_count; |
| 10836 | 541 } |
| 542 | |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
543 *data_size = mode_par->frames_per_packet * SUBFR_SIZE * |
|
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
544 mode_par->subframe_count * sizeof(float); |
| 10836 | 545 |
|
10872
a4fbfc917c2e
Remove the struct SiprModeParam of the context. This will simplify splitting
vitor
parents:
10871
diff
changeset
|
546 return mode_par->bits_per_frame >> 3; |
| 10836 | 547 }; |
| 548 | |
| 549 AVCodec sipr_decoder = { | |
| 550 "sipr", | |
| 551 CODEC_TYPE_AUDIO, | |
| 552 CODEC_ID_SIPR, | |
| 553 sizeof(SiprContext), | |
| 554 sipr_decoder_init, | |
| 555 NULL, | |
| 556 NULL, | |
| 557 sipr_decode_frame, | |
| 558 .long_name = NULL_IF_CONFIG_SMALL("RealAudio SIPR / ACELP.NET"), | |
| 559 }; |
