00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/common.h"
00028 #include "libavutil/lfg.h"
00029
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "lsp.h"
00033 #include "celp_filters.h"
00034 #include "celp_math.h"
00035 #include "acelp_filters.h"
00036 #include "acelp_vectors.h"
00037 #include "acelp_pitch_delay.h"
00038
00039 #define AMR_USE_16BIT_TABLES
00040 #include "amr.h"
00041
00042 #include "amrwbdata.h"
00043 #include "mips/amrwbdec_mips.h"
00044
00045 typedef struct {
00046 AVFrame avframe;
00047 AMRWBFrame frame;
00048 enum Mode fr_cur_mode;
00049 uint8_t fr_quality;
00050 float isf_cur[LP_ORDER];
00051 float isf_q_past[LP_ORDER];
00052 float isf_past_final[LP_ORDER];
00053 double isp[4][LP_ORDER];
00054 double isp_sub4_past[LP_ORDER];
00055
00056 float lp_coef[4][LP_ORDER];
00057
00058 uint8_t base_pitch_lag;
00059 uint8_t pitch_lag_int;
00060
00061 float excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 2 + AMRWB_SFR_SIZE];
00062 float *excitation;
00063
00064 float pitch_vector[AMRWB_SFR_SIZE];
00065 float fixed_vector[AMRWB_SFR_SIZE];
00066
00067 float prediction_error[4];
00068 float pitch_gain[6];
00069 float fixed_gain[2];
00070
00071 float tilt_coef;
00072
00073 float prev_sparse_fixed_gain;
00074 uint8_t prev_ir_filter_nr;
00075 float prev_tr_gain;
00076
00077 float samples_az[LP_ORDER + AMRWB_SFR_SIZE];
00078 float samples_up[UPS_MEM_SIZE + AMRWB_SFR_SIZE];
00079 float samples_hb[LP_ORDER_16k + AMRWB_SFR_SIZE_16k];
00080
00081 float hpf_31_mem[2], hpf_400_mem[2];
00082 float demph_mem[1];
00083 float bpf_6_7_mem[HB_FIR_SIZE];
00084 float lpf_7_mem[HB_FIR_SIZE];
00085
00086 AVLFG prng;
00087 uint8_t first_frame;
00088 ACELPFContext acelpf_ctx;
00089 ACELPVContext acelpv_ctx;
00090 CELPFContext celpf_ctx;
00091 CELPMContext celpm_ctx;
00092
00093 } AMRWBContext;
00094
00095 static av_cold int amrwb_decode_init(AVCodecContext *avctx)
00096 {
00097 AMRWBContext *ctx = avctx->priv_data;
00098 int i;
00099
00100 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00101
00102 av_lfg_init(&ctx->prng, 1);
00103
00104 ctx->excitation = &ctx->excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 1];
00105 ctx->first_frame = 1;
00106
00107 for (i = 0; i < LP_ORDER; i++)
00108 ctx->isf_past_final[i] = isf_init[i] * (1.0f / (1 << 15));
00109
00110 for (i = 0; i < 4; i++)
00111 ctx->prediction_error[i] = MIN_ENERGY;
00112
00113 avcodec_get_frame_defaults(&ctx->avframe);
00114 avctx->coded_frame = &ctx->avframe;
00115
00116 ff_acelp_filter_init(&ctx->acelpf_ctx);
00117 ff_acelp_vectors_init(&ctx->acelpv_ctx);
00118 ff_celp_filter_init(&ctx->celpf_ctx);
00119 ff_celp_math_init(&ctx->celpm_ctx);
00120
00121 return 0;
00122 }
00123
00133 static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf)
00134 {
00135
00136 ctx->fr_cur_mode = buf[0] >> 3 & 0x0F;
00137 ctx->fr_quality = (buf[0] & 0x4) == 0x4;
00138
00139 return 1;
00140 }
00141
00149 static void decode_isf_indices_36b(uint16_t *ind, float *isf_q)
00150 {
00151 int i;
00152
00153 for (i = 0; i < 9; i++)
00154 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
00155
00156 for (i = 0; i < 7; i++)
00157 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
00158
00159 for (i = 0; i < 5; i++)
00160 isf_q[i] += dico21_isf_36b[ind[2]][i] * (1.0f / (1 << 15));
00161
00162 for (i = 0; i < 4; i++)
00163 isf_q[i + 5] += dico22_isf_36b[ind[3]][i] * (1.0f / (1 << 15));
00164
00165 for (i = 0; i < 7; i++)
00166 isf_q[i + 9] += dico23_isf_36b[ind[4]][i] * (1.0f / (1 << 15));
00167 }
00168
00176 static void decode_isf_indices_46b(uint16_t *ind, float *isf_q)
00177 {
00178 int i;
00179
00180 for (i = 0; i < 9; i++)
00181 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
00182
00183 for (i = 0; i < 7; i++)
00184 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
00185
00186 for (i = 0; i < 3; i++)
00187 isf_q[i] += dico21_isf[ind[2]][i] * (1.0f / (1 << 15));
00188
00189 for (i = 0; i < 3; i++)
00190 isf_q[i + 3] += dico22_isf[ind[3]][i] * (1.0f / (1 << 15));
00191
00192 for (i = 0; i < 3; i++)
00193 isf_q[i + 6] += dico23_isf[ind[4]][i] * (1.0f / (1 << 15));
00194
00195 for (i = 0; i < 3; i++)
00196 isf_q[i + 9] += dico24_isf[ind[5]][i] * (1.0f / (1 << 15));
00197
00198 for (i = 0; i < 4; i++)
00199 isf_q[i + 12] += dico25_isf[ind[6]][i] * (1.0f / (1 << 15));
00200 }
00201
00210 static void isf_add_mean_and_past(float *isf_q, float *isf_past)
00211 {
00212 int i;
00213 float tmp;
00214
00215 for (i = 0; i < LP_ORDER; i++) {
00216 tmp = isf_q[i];
00217 isf_q[i] += isf_mean[i] * (1.0f / (1 << 15));
00218 isf_q[i] += PRED_FACTOR * isf_past[i];
00219 isf_past[i] = tmp;
00220 }
00221 }
00222
00230 static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past)
00231 {
00232 int i, k;
00233
00234 for (k = 0; k < 3; k++) {
00235 float c = isfp_inter[k];
00236 for (i = 0; i < LP_ORDER; i++)
00237 isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i];
00238 }
00239 }
00240
00252 static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index,
00253 uint8_t *base_lag_int, int subframe)
00254 {
00255 if (subframe == 0 || subframe == 2) {
00256 if (pitch_index < 376) {
00257 *lag_int = (pitch_index + 137) >> 2;
00258 *lag_frac = pitch_index - (*lag_int << 2) + 136;
00259 } else if (pitch_index < 440) {
00260 *lag_int = (pitch_index + 257 - 376) >> 1;
00261 *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) << 1;
00262
00263 } else {
00264 *lag_int = pitch_index - 280;
00265 *lag_frac = 0;
00266 }
00267
00268 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
00269 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15);
00270
00271
00272
00273 } else {
00274 *lag_int = (pitch_index + 1) >> 2;
00275 *lag_frac = pitch_index - (*lag_int << 2);
00276 *lag_int += *base_lag_int;
00277 }
00278 }
00279
00285 static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index,
00286 uint8_t *base_lag_int, int subframe, enum Mode mode)
00287 {
00288 if (subframe == 0 || (subframe == 2 && mode != MODE_6k60)) {
00289 if (pitch_index < 116) {
00290 *lag_int = (pitch_index + 69) >> 1;
00291 *lag_frac = (pitch_index - (*lag_int << 1) + 68) << 1;
00292 } else {
00293 *lag_int = pitch_index - 24;
00294 *lag_frac = 0;
00295 }
00296
00297 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
00298 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15);
00299 } else {
00300 *lag_int = (pitch_index + 1) >> 1;
00301 *lag_frac = (pitch_index - (*lag_int << 1)) << 1;
00302 *lag_int += *base_lag_int;
00303 }
00304 }
00305
00314 static void decode_pitch_vector(AMRWBContext *ctx,
00315 const AMRWBSubFrame *amr_subframe,
00316 const int subframe)
00317 {
00318 int pitch_lag_int, pitch_lag_frac;
00319 int i;
00320 float *exc = ctx->excitation;
00321 enum Mode mode = ctx->fr_cur_mode;
00322
00323 if (mode <= MODE_8k85) {
00324 decode_pitch_lag_low(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
00325 &ctx->base_pitch_lag, subframe, mode);
00326 } else
00327 decode_pitch_lag_high(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
00328 &ctx->base_pitch_lag, subframe);
00329
00330 ctx->pitch_lag_int = pitch_lag_int;
00331 pitch_lag_int += pitch_lag_frac > 0;
00332
00333
00334
00335 ctx->acelpf_ctx.acelp_interpolatef(exc,
00336 exc + 1 - pitch_lag_int,
00337 ac_inter, 4,
00338 pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4),
00339 LP_ORDER, AMRWB_SFR_SIZE + 1);
00340
00341
00342
00343 if (amr_subframe->ltp) {
00344 memcpy(ctx->pitch_vector, exc, AMRWB_SFR_SIZE * sizeof(float));
00345 } else {
00346 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00347 ctx->pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] +
00348 0.18 * exc[i + 1];
00349 memcpy(exc, ctx->pitch_vector, AMRWB_SFR_SIZE * sizeof(float));
00350 }
00351 }
00352
00354 #define BIT_STR(x,lsb,len) (((x) >> (lsb)) & ((1 << (len)) - 1))
00355
00357 #define BIT_POS(x, p) (((x) >> (p)) & 1)
00358
00372 static inline void decode_1p_track(int *out, int code, int m, int off)
00373 {
00374 int pos = BIT_STR(code, 0, m) + off;
00375
00376 out[0] = BIT_POS(code, m) ? -pos : pos;
00377 }
00378
00379 static inline void decode_2p_track(int *out, int code, int m, int off)
00380 {
00381 int pos0 = BIT_STR(code, m, m) + off;
00382 int pos1 = BIT_STR(code, 0, m) + off;
00383
00384 out[0] = BIT_POS(code, 2*m) ? -pos0 : pos0;
00385 out[1] = BIT_POS(code, 2*m) ? -pos1 : pos1;
00386 out[1] = pos0 > pos1 ? -out[1] : out[1];
00387 }
00388
00389 static void decode_3p_track(int *out, int code, int m, int off)
00390 {
00391 int half_2p = BIT_POS(code, 2*m - 1) << (m - 1);
00392
00393 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
00394 m - 1, off + half_2p);
00395 decode_1p_track(out + 2, BIT_STR(code, 2*m, m + 1), m, off);
00396 }
00397
00398 static void decode_4p_track(int *out, int code, int m, int off)
00399 {
00400 int half_4p, subhalf_2p;
00401 int b_offset = 1 << (m - 1);
00402
00403 switch (BIT_STR(code, 4*m - 2, 2)) {
00404 case 0:
00405 half_4p = BIT_POS(code, 4*m - 3) << (m - 1);
00406 subhalf_2p = BIT_POS(code, 2*m - 3) << (m - 2);
00407
00408 decode_2p_track(out, BIT_STR(code, 0, 2*m - 3),
00409 m - 2, off + half_4p + subhalf_2p);
00410 decode_2p_track(out + 2, BIT_STR(code, 2*m - 2, 2*m - 1),
00411 m - 1, off + half_4p);
00412 break;
00413 case 1:
00414 decode_1p_track(out, BIT_STR(code, 3*m - 2, m),
00415 m - 1, off);
00416 decode_3p_track(out + 1, BIT_STR(code, 0, 3*m - 2),
00417 m - 1, off + b_offset);
00418 break;
00419 case 2:
00420 decode_2p_track(out, BIT_STR(code, 2*m - 1, 2*m - 1),
00421 m - 1, off);
00422 decode_2p_track(out + 2, BIT_STR(code, 0, 2*m - 1),
00423 m - 1, off + b_offset);
00424 break;
00425 case 3:
00426 decode_3p_track(out, BIT_STR(code, m, 3*m - 2),
00427 m - 1, off);
00428 decode_1p_track(out + 3, BIT_STR(code, 0, m),
00429 m - 1, off + b_offset);
00430 break;
00431 }
00432 }
00433
00434 static void decode_5p_track(int *out, int code, int m, int off)
00435 {
00436 int half_3p = BIT_POS(code, 5*m - 1) << (m - 1);
00437
00438 decode_3p_track(out, BIT_STR(code, 2*m + 1, 3*m - 2),
00439 m - 1, off + half_3p);
00440
00441 decode_2p_track(out + 3, BIT_STR(code, 0, 2*m + 1), m, off);
00442 }
00443
00444 static void decode_6p_track(int *out, int code, int m, int off)
00445 {
00446 int b_offset = 1 << (m - 1);
00447
00448 int half_more = BIT_POS(code, 6*m - 5) << (m - 1);
00449 int half_other = b_offset - half_more;
00450
00451 switch (BIT_STR(code, 6*m - 4, 2)) {
00452 case 0:
00453 decode_1p_track(out, BIT_STR(code, 0, m),
00454 m - 1, off + half_more);
00455 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
00456 m - 1, off + half_more);
00457 break;
00458 case 1:
00459 decode_1p_track(out, BIT_STR(code, 0, m),
00460 m - 1, off + half_other);
00461 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
00462 m - 1, off + half_more);
00463 break;
00464 case 2:
00465 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
00466 m - 1, off + half_other);
00467 decode_4p_track(out + 2, BIT_STR(code, 2*m - 1, 4*m - 4),
00468 m - 1, off + half_more);
00469 break;
00470 case 3:
00471 decode_3p_track(out, BIT_STR(code, 3*m - 2, 3*m - 2),
00472 m - 1, off);
00473 decode_3p_track(out + 3, BIT_STR(code, 0, 3*m - 2),
00474 m - 1, off + b_offset);
00475 break;
00476 }
00477 }
00478
00488 static void decode_fixed_vector(float *fixed_vector, const uint16_t *pulse_hi,
00489 const uint16_t *pulse_lo, const enum Mode mode)
00490 {
00491
00492
00493 int sig_pos[4][6];
00494 int spacing = (mode == MODE_6k60) ? 2 : 4;
00495 int i, j;
00496
00497 switch (mode) {
00498 case MODE_6k60:
00499 for (i = 0; i < 2; i++)
00500 decode_1p_track(sig_pos[i], pulse_lo[i], 5, 1);
00501 break;
00502 case MODE_8k85:
00503 for (i = 0; i < 4; i++)
00504 decode_1p_track(sig_pos[i], pulse_lo[i], 4, 1);
00505 break;
00506 case MODE_12k65:
00507 for (i = 0; i < 4; i++)
00508 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
00509 break;
00510 case MODE_14k25:
00511 for (i = 0; i < 2; i++)
00512 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
00513 for (i = 2; i < 4; i++)
00514 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
00515 break;
00516 case MODE_15k85:
00517 for (i = 0; i < 4; i++)
00518 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
00519 break;
00520 case MODE_18k25:
00521 for (i = 0; i < 4; i++)
00522 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
00523 ((int) pulse_hi[i] << 14), 4, 1);
00524 break;
00525 case MODE_19k85:
00526 for (i = 0; i < 2; i++)
00527 decode_5p_track(sig_pos[i], (int) pulse_lo[i] +
00528 ((int) pulse_hi[i] << 10), 4, 1);
00529 for (i = 2; i < 4; i++)
00530 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
00531 ((int) pulse_hi[i] << 14), 4, 1);
00532 break;
00533 case MODE_23k05:
00534 case MODE_23k85:
00535 for (i = 0; i < 4; i++)
00536 decode_6p_track(sig_pos[i], (int) pulse_lo[i] +
00537 ((int) pulse_hi[i] << 11), 4, 1);
00538 break;
00539 }
00540
00541 memset(fixed_vector, 0, sizeof(float) * AMRWB_SFR_SIZE);
00542
00543 for (i = 0; i < 4; i++)
00544 for (j = 0; j < pulses_nb_per_mode_tr[mode][i]; j++) {
00545 int pos = (FFABS(sig_pos[i][j]) - 1) * spacing + i;
00546
00547 fixed_vector[pos] += sig_pos[i][j] < 0 ? -1.0 : 1.0;
00548 }
00549 }
00550
00559 static void decode_gains(const uint8_t vq_gain, const enum Mode mode,
00560 float *fixed_gain_factor, float *pitch_gain)
00561 {
00562 const int16_t *gains = (mode <= MODE_8k85 ? qua_gain_6b[vq_gain] :
00563 qua_gain_7b[vq_gain]);
00564
00565 *pitch_gain = gains[0] * (1.0f / (1 << 14));
00566 *fixed_gain_factor = gains[1] * (1.0f / (1 << 11));
00567 }
00568
00575
00576
00577 static void pitch_sharpening(AMRWBContext *ctx, float *fixed_vector)
00578 {
00579 int i;
00580
00581
00582 for (i = AMRWB_SFR_SIZE - 1; i != 0; i--)
00583 fixed_vector[i] -= fixed_vector[i - 1] * ctx->tilt_coef;
00584
00585
00586 for (i = ctx->pitch_lag_int; i < AMRWB_SFR_SIZE; i++)
00587 fixed_vector[i] += fixed_vector[i - ctx->pitch_lag_int] * 0.85;
00588 }
00589
00597
00598
00599 static float voice_factor(float *p_vector, float p_gain,
00600 float *f_vector, float f_gain,
00601 CELPMContext *ctx)
00602 {
00603 double p_ener = (double) ctx->dot_productf(p_vector, p_vector,
00604 AMRWB_SFR_SIZE) *
00605 p_gain * p_gain;
00606 double f_ener = (double) ctx->dot_productf(f_vector, f_vector,
00607 AMRWB_SFR_SIZE) *
00608 f_gain * f_gain;
00609
00610 return (p_ener - f_ener) / (p_ener + f_ener);
00611 }
00612
00623 static float *anti_sparseness(AMRWBContext *ctx,
00624 float *fixed_vector, float *buf)
00625 {
00626 int ir_filter_nr;
00627
00628 if (ctx->fr_cur_mode > MODE_8k85)
00629 return fixed_vector;
00630
00631 if (ctx->pitch_gain[0] < 0.6) {
00632 ir_filter_nr = 0;
00633 } else if (ctx->pitch_gain[0] < 0.9) {
00634 ir_filter_nr = 1;
00635 } else
00636 ir_filter_nr = 2;
00637
00638
00639 if (ctx->fixed_gain[0] > 3.0 * ctx->fixed_gain[1]) {
00640 if (ir_filter_nr < 2)
00641 ir_filter_nr++;
00642 } else {
00643 int i, count = 0;
00644
00645 for (i = 0; i < 6; i++)
00646 if (ctx->pitch_gain[i] < 0.6)
00647 count++;
00648
00649 if (count > 2)
00650 ir_filter_nr = 0;
00651
00652 if (ir_filter_nr > ctx->prev_ir_filter_nr + 1)
00653 ir_filter_nr--;
00654 }
00655
00656
00657 ctx->prev_ir_filter_nr = ir_filter_nr;
00658
00659 ir_filter_nr += (ctx->fr_cur_mode == MODE_8k85);
00660
00661 if (ir_filter_nr < 2) {
00662 int i;
00663 const float *coef = ir_filters_lookup[ir_filter_nr];
00664
00665
00666
00667
00668
00669
00670
00671
00672 memset(buf, 0, sizeof(float) * AMRWB_SFR_SIZE);
00673 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00674 if (fixed_vector[i])
00675 ff_celp_circ_addf(buf, buf, coef, i, fixed_vector[i],
00676 AMRWB_SFR_SIZE);
00677 fixed_vector = buf;
00678 }
00679
00680 return fixed_vector;
00681 }
00682
00687 static float stability_factor(const float *isf, const float *isf_past)
00688 {
00689 int i;
00690 float acc = 0.0;
00691
00692 for (i = 0; i < LP_ORDER - 1; i++)
00693 acc += (isf[i] - isf_past[i]) * (isf[i] - isf_past[i]);
00694
00695
00696
00697 return FFMAX(0.0, 1.25 - acc * 0.8 * 512);
00698 }
00699
00711 static float noise_enhancer(float fixed_gain, float *prev_tr_gain,
00712 float voice_fac, float stab_fac)
00713 {
00714 float sm_fac = 0.5 * (1 - voice_fac) * stab_fac;
00715 float g0;
00716
00717
00718
00719
00720 if (fixed_gain < *prev_tr_gain) {
00721 g0 = FFMIN(*prev_tr_gain, fixed_gain + fixed_gain *
00722 (6226 * (1.0f / (1 << 15))));
00723 } else
00724 g0 = FFMAX(*prev_tr_gain, fixed_gain *
00725 (27536 * (1.0f / (1 << 15))));
00726
00727 *prev_tr_gain = g0;
00728
00729 return sm_fac * g0 + (1 - sm_fac) * fixed_gain;
00730 }
00731
00738 static void pitch_enhancer(float *fixed_vector, float voice_fac)
00739 {
00740 int i;
00741 float cpe = 0.125 * (1 + voice_fac);
00742 float last = fixed_vector[0];
00743
00744 fixed_vector[0] -= cpe * fixed_vector[1];
00745
00746 for (i = 1; i < AMRWB_SFR_SIZE - 1; i++) {
00747 float cur = fixed_vector[i];
00748
00749 fixed_vector[i] -= cpe * (last + fixed_vector[i + 1]);
00750 last = cur;
00751 }
00752
00753 fixed_vector[AMRWB_SFR_SIZE - 1] -= cpe * last;
00754 }
00755
00766 static void synthesis(AMRWBContext *ctx, float *lpc, float *excitation,
00767 float fixed_gain, const float *fixed_vector,
00768 float *samples)
00769 {
00770 ctx->acelpv_ctx.weighted_vector_sumf(excitation, ctx->pitch_vector, fixed_vector,
00771 ctx->pitch_gain[0], fixed_gain, AMRWB_SFR_SIZE);
00772
00773
00774 if (ctx->pitch_gain[0] > 0.5 && ctx->fr_cur_mode <= MODE_8k85) {
00775 int i;
00776 float energy = ctx->celpm_ctx.dot_productf(excitation, excitation,
00777 AMRWB_SFR_SIZE);
00778
00779
00780
00781 float pitch_factor = 0.25 * ctx->pitch_gain[0] * ctx->pitch_gain[0];
00782
00783 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00784 excitation[i] += pitch_factor * ctx->pitch_vector[i];
00785
00786 ff_scale_vector_to_given_sum_of_squares(excitation, excitation,
00787 energy, AMRWB_SFR_SIZE);
00788 }
00789
00790 ctx->celpf_ctx.celp_lp_synthesis_filterf(samples, lpc, excitation,
00791 AMRWB_SFR_SIZE, LP_ORDER);
00792 }
00793
00803 static void de_emphasis(float *out, float *in, float m, float mem[1])
00804 {
00805 int i;
00806
00807 out[0] = in[0] + m * mem[0];
00808
00809 for (i = 1; i < AMRWB_SFR_SIZE; i++)
00810 out[i] = in[i] + out[i - 1] * m;
00811
00812 mem[0] = out[AMRWB_SFR_SIZE - 1];
00813 }
00814
00824 static void upsample_5_4(float *out, const float *in, int o_size, CELPMContext *ctx)
00825 {
00826 const float *in0 = in - UPS_FIR_SIZE + 1;
00827 int i, j, k;
00828 int int_part = 0, frac_part;
00829
00830 i = 0;
00831 for (j = 0; j < o_size / 5; j++) {
00832 out[i] = in[int_part];
00833 frac_part = 4;
00834 i++;
00835
00836 for (k = 1; k < 5; k++) {
00837 out[i] = ctx->dot_productf(in0 + int_part,
00838 upsample_fir[4 - frac_part],
00839 UPS_MEM_SIZE);
00840 int_part++;
00841 frac_part--;
00842 i++;
00843 }
00844 }
00845 }
00846
00856 static float find_hb_gain(AMRWBContext *ctx, const float *synth,
00857 uint16_t hb_idx, uint8_t vad)
00858 {
00859 int wsp = (vad > 0);
00860 float tilt;
00861
00862 if (ctx->fr_cur_mode == MODE_23k85)
00863 return qua_hb_gain[hb_idx] * (1.0f / (1 << 14));
00864
00865 tilt = ctx->celpm_ctx.dot_productf(synth, synth + 1, AMRWB_SFR_SIZE - 1) /
00866 ctx->celpm_ctx.dot_productf(synth, synth, AMRWB_SFR_SIZE);
00867
00868
00869 return av_clipf((1.0 - FFMAX(0.0, tilt)) * (1.25 - 0.25 * wsp), 0.1, 1.0);
00870 }
00871
00881 static void scaled_hb_excitation(AMRWBContext *ctx, float *hb_exc,
00882 const float *synth_exc, float hb_gain)
00883 {
00884 int i;
00885 float energy = ctx->celpm_ctx.dot_productf(synth_exc, synth_exc, AMRWB_SFR_SIZE);
00886
00887
00888 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
00889 hb_exc[i] = 32768.0 - (uint16_t) av_lfg_get(&ctx->prng);
00890
00891 ff_scale_vector_to_given_sum_of_squares(hb_exc, hb_exc,
00892 energy * hb_gain * hb_gain,
00893 AMRWB_SFR_SIZE_16k);
00894 }
00895
00899 static float auto_correlation(float *diff_isf, float mean, int lag)
00900 {
00901 int i;
00902 float sum = 0.0;
00903
00904 for (i = 7; i < LP_ORDER - 2; i++) {
00905 float prod = (diff_isf[i] - mean) * (diff_isf[i - lag] - mean);
00906 sum += prod * prod;
00907 }
00908 return sum;
00909 }
00910
00918 static void extrapolate_isf(float isf[LP_ORDER_16k])
00919 {
00920 float diff_isf[LP_ORDER - 2], diff_mean;
00921 float *diff_hi = diff_isf - LP_ORDER + 1;
00922 float corr_lag[3];
00923 float est, scale;
00924 int i, i_max_corr;
00925
00926 isf[LP_ORDER_16k - 1] = isf[LP_ORDER - 1];
00927
00928
00929 for (i = 0; i < LP_ORDER - 2; i++)
00930 diff_isf[i] = isf[i + 1] - isf[i];
00931
00932 diff_mean = 0.0;
00933 for (i = 2; i < LP_ORDER - 2; i++)
00934 diff_mean += diff_isf[i] * (1.0f / (LP_ORDER - 4));
00935
00936
00937 i_max_corr = 0;
00938 for (i = 0; i < 3; i++) {
00939 corr_lag[i] = auto_correlation(diff_isf, diff_mean, i + 2);
00940
00941 if (corr_lag[i] > corr_lag[i_max_corr])
00942 i_max_corr = i;
00943 }
00944 i_max_corr++;
00945
00946 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00947 isf[i] = isf[i - 1] + isf[i - 1 - i_max_corr]
00948 - isf[i - 2 - i_max_corr];
00949
00950
00951 est = 7965 + (isf[2] - isf[3] - isf[4]) / 6.0;
00952 scale = 0.5 * (FFMIN(est, 7600) - isf[LP_ORDER - 2]) /
00953 (isf[LP_ORDER_16k - 2] - isf[LP_ORDER - 2]);
00954
00955 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00956 diff_hi[i] = scale * (isf[i] - isf[i - 1]);
00957
00958
00959 for (i = LP_ORDER; i < LP_ORDER_16k - 1; i++)
00960 if (diff_hi[i] + diff_hi[i - 1] < 5.0) {
00961 if (diff_hi[i] > diff_hi[i - 1]) {
00962 diff_hi[i - 1] = 5.0 - diff_hi[i];
00963 } else
00964 diff_hi[i] = 5.0 - diff_hi[i - 1];
00965 }
00966
00967 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00968 isf[i] = isf[i - 1] + diff_hi[i] * (1.0f / (1 << 15));
00969
00970
00971 for (i = 0; i < LP_ORDER_16k - 1; i++)
00972 isf[i] *= 0.8;
00973 }
00974
00984 static void lpc_weighting(float *out, const float *lpc, float gamma, int size)
00985 {
00986 int i;
00987 float fac = gamma;
00988
00989 for (i = 0; i < size; i++) {
00990 out[i] = lpc[i] * fac;
00991 fac *= gamma;
00992 }
00993 }
00994
01006 static void hb_synthesis(AMRWBContext *ctx, int subframe, float *samples,
01007 const float *exc, const float *isf, const float *isf_past)
01008 {
01009 float hb_lpc[LP_ORDER_16k];
01010 enum Mode mode = ctx->fr_cur_mode;
01011
01012 if (mode == MODE_6k60) {
01013 float e_isf[LP_ORDER_16k];
01014 double e_isp[LP_ORDER_16k];
01015
01016 ctx->acelpv_ctx.weighted_vector_sumf(e_isf, isf_past, isf, isfp_inter[subframe],
01017 1.0 - isfp_inter[subframe], LP_ORDER);
01018
01019 extrapolate_isf(e_isf);
01020
01021 e_isf[LP_ORDER_16k - 1] *= 2.0;
01022 ff_acelp_lsf2lspd(e_isp, e_isf, LP_ORDER_16k);
01023 ff_amrwb_lsp2lpc(e_isp, hb_lpc, LP_ORDER_16k);
01024
01025 lpc_weighting(hb_lpc, hb_lpc, 0.9, LP_ORDER_16k);
01026 } else {
01027 lpc_weighting(hb_lpc, ctx->lp_coef[subframe], 0.6, LP_ORDER);
01028 }
01029
01030 ctx->celpf_ctx.celp_lp_synthesis_filterf(samples, hb_lpc, exc, AMRWB_SFR_SIZE_16k,
01031 (mode == MODE_6k60) ? LP_ORDER_16k : LP_ORDER);
01032 }
01033
01046 #ifndef hb_fir_filter
01047 static void hb_fir_filter(float *out, const float fir_coef[HB_FIR_SIZE + 1],
01048 float mem[HB_FIR_SIZE], const float *in)
01049 {
01050 int i, j;
01051 float data[AMRWB_SFR_SIZE_16k + HB_FIR_SIZE];
01052
01053 memcpy(data, mem, HB_FIR_SIZE * sizeof(float));
01054 memcpy(data + HB_FIR_SIZE, in, AMRWB_SFR_SIZE_16k * sizeof(float));
01055
01056 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++) {
01057 out[i] = 0.0;
01058 for (j = 0; j <= HB_FIR_SIZE; j++)
01059 out[i] += data[i + j] * fir_coef[j];
01060 }
01061
01062 memcpy(mem, data + AMRWB_SFR_SIZE_16k, HB_FIR_SIZE * sizeof(float));
01063 }
01064 #endif
01065
01069 static void update_sub_state(AMRWBContext *ctx)
01070 {
01071 memmove(&ctx->excitation_buf[0], &ctx->excitation_buf[AMRWB_SFR_SIZE],
01072 (AMRWB_P_DELAY_MAX + LP_ORDER + 1) * sizeof(float));
01073
01074 memmove(&ctx->pitch_gain[1], &ctx->pitch_gain[0], 5 * sizeof(float));
01075 memmove(&ctx->fixed_gain[1], &ctx->fixed_gain[0], 1 * sizeof(float));
01076
01077 memmove(&ctx->samples_az[0], &ctx->samples_az[AMRWB_SFR_SIZE],
01078 LP_ORDER * sizeof(float));
01079 memmove(&ctx->samples_up[0], &ctx->samples_up[AMRWB_SFR_SIZE],
01080 UPS_MEM_SIZE * sizeof(float));
01081 memmove(&ctx->samples_hb[0], &ctx->samples_hb[AMRWB_SFR_SIZE_16k],
01082 LP_ORDER_16k * sizeof(float));
01083 }
01084
01085 static int amrwb_decode_frame(AVCodecContext *avctx, void *data,
01086 int *got_frame_ptr, AVPacket *avpkt)
01087 {
01088 AMRWBContext *ctx = avctx->priv_data;
01089 AMRWBFrame *cf = &ctx->frame;
01090 const uint8_t *buf = avpkt->data;
01091 int buf_size = avpkt->size;
01092 int expected_fr_size, header_size;
01093 float *buf_out;
01094 float spare_vector[AMRWB_SFR_SIZE];
01095 float fixed_gain_factor;
01096 float *synth_fixed_vector;
01097 float synth_fixed_gain;
01098 float voice_fac, stab_fac;
01099 float synth_exc[AMRWB_SFR_SIZE];
01100 float hb_exc[AMRWB_SFR_SIZE_16k];
01101 float hb_samples[AMRWB_SFR_SIZE_16k];
01102 float hb_gain;
01103 int sub, i, ret;
01104
01105
01106 ctx->avframe.nb_samples = 4 * AMRWB_SFR_SIZE_16k;
01107 if ((ret = avctx->get_buffer(avctx, &ctx->avframe)) < 0) {
01108 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01109 return ret;
01110 }
01111 buf_out = (float *)ctx->avframe.data[0];
01112
01113 header_size = decode_mime_header(ctx, buf);
01114 if (ctx->fr_cur_mode > MODE_SID) {
01115 av_log(avctx, AV_LOG_ERROR,
01116 "Invalid mode %d\n", ctx->fr_cur_mode);
01117 return AVERROR_INVALIDDATA;
01118 }
01119 expected_fr_size = ((cf_sizes_wb[ctx->fr_cur_mode] + 7) >> 3) + 1;
01120
01121 if (buf_size < expected_fr_size) {
01122 av_log(avctx, AV_LOG_ERROR,
01123 "Frame too small (%d bytes). Truncated file?\n", buf_size);
01124 *got_frame_ptr = 0;
01125 return AVERROR_INVALIDDATA;
01126 }
01127
01128 if (!ctx->fr_quality || ctx->fr_cur_mode > MODE_SID)
01129 av_log(avctx, AV_LOG_ERROR, "Encountered a bad or corrupted frame\n");
01130
01131 if (ctx->fr_cur_mode == MODE_SID) {
01132 av_log_missing_feature(avctx, "SID mode", 1);
01133 return -1;
01134 }
01135
01136 ff_amr_bit_reorder((uint16_t *) &ctx->frame, sizeof(AMRWBFrame),
01137 buf + header_size, amr_bit_orderings_by_mode[ctx->fr_cur_mode]);
01138
01139
01140 if (ctx->fr_cur_mode == MODE_6k60) {
01141 decode_isf_indices_36b(cf->isp_id, ctx->isf_cur);
01142 } else {
01143 decode_isf_indices_46b(cf->isp_id, ctx->isf_cur);
01144 }
01145
01146 isf_add_mean_and_past(ctx->isf_cur, ctx->isf_q_past);
01147 ff_set_min_dist_lsf(ctx->isf_cur, MIN_ISF_SPACING, LP_ORDER - 1);
01148
01149 stab_fac = stability_factor(ctx->isf_cur, ctx->isf_past_final);
01150
01151 ctx->isf_cur[LP_ORDER - 1] *= 2.0;
01152 ff_acelp_lsf2lspd(ctx->isp[3], ctx->isf_cur, LP_ORDER);
01153
01154
01155 if (ctx->first_frame) {
01156 ctx->first_frame = 0;
01157 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(double));
01158 }
01159 interpolate_isp(ctx->isp, ctx->isp_sub4_past);
01160
01161 for (sub = 0; sub < 4; sub++)
01162 ff_amrwb_lsp2lpc(ctx->isp[sub], ctx->lp_coef[sub], LP_ORDER);
01163
01164 for (sub = 0; sub < 4; sub++) {
01165 const AMRWBSubFrame *cur_subframe = &cf->subframe[sub];
01166 float *sub_buf = buf_out + sub * AMRWB_SFR_SIZE_16k;
01167
01168
01169 decode_pitch_vector(ctx, cur_subframe, sub);
01170
01171 decode_fixed_vector(ctx->fixed_vector, cur_subframe->pul_ih,
01172 cur_subframe->pul_il, ctx->fr_cur_mode);
01173
01174 pitch_sharpening(ctx, ctx->fixed_vector);
01175
01176 decode_gains(cur_subframe->vq_gain, ctx->fr_cur_mode,
01177 &fixed_gain_factor, &ctx->pitch_gain[0]);
01178
01179 ctx->fixed_gain[0] =
01180 ff_amr_set_fixed_gain(fixed_gain_factor,
01181 ctx->celpm_ctx.dot_productf(ctx->fixed_vector,
01182 ctx->fixed_vector,
01183 AMRWB_SFR_SIZE) /
01184 AMRWB_SFR_SIZE,
01185 ctx->prediction_error,
01186 ENERGY_MEAN, energy_pred_fac);
01187
01188
01189 voice_fac = voice_factor(ctx->pitch_vector, ctx->pitch_gain[0],
01190 ctx->fixed_vector, ctx->fixed_gain[0],
01191 &ctx->celpm_ctx);
01192 ctx->tilt_coef = voice_fac * 0.25 + 0.25;
01193
01194
01195 for (i = 0; i < AMRWB_SFR_SIZE; i++) {
01196 ctx->excitation[i] *= ctx->pitch_gain[0];
01197 ctx->excitation[i] += ctx->fixed_gain[0] * ctx->fixed_vector[i];
01198 ctx->excitation[i] = truncf(ctx->excitation[i]);
01199 }
01200
01201
01202 synth_fixed_gain = noise_enhancer(ctx->fixed_gain[0], &ctx->prev_tr_gain,
01203 voice_fac, stab_fac);
01204
01205 synth_fixed_vector = anti_sparseness(ctx, ctx->fixed_vector,
01206 spare_vector);
01207
01208 pitch_enhancer(synth_fixed_vector, voice_fac);
01209
01210 synthesis(ctx, ctx->lp_coef[sub], synth_exc, synth_fixed_gain,
01211 synth_fixed_vector, &ctx->samples_az[LP_ORDER]);
01212
01213
01214 de_emphasis(&ctx->samples_up[UPS_MEM_SIZE],
01215 &ctx->samples_az[LP_ORDER], PREEMPH_FAC, ctx->demph_mem);
01216
01217 ctx->acelpf_ctx.acelp_apply_order_2_transfer_function(&ctx->samples_up[UPS_MEM_SIZE],
01218 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_31_poles,
01219 hpf_31_gain, ctx->hpf_31_mem, AMRWB_SFR_SIZE);
01220
01221 upsample_5_4(sub_buf, &ctx->samples_up[UPS_FIR_SIZE],
01222 AMRWB_SFR_SIZE_16k, &ctx->celpm_ctx);
01223
01224
01225 ctx->acelpf_ctx.acelp_apply_order_2_transfer_function(hb_samples,
01226 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_400_poles,
01227 hpf_400_gain, ctx->hpf_400_mem, AMRWB_SFR_SIZE);
01228
01229 hb_gain = find_hb_gain(ctx, hb_samples,
01230 cur_subframe->hb_gain, cf->vad);
01231
01232 scaled_hb_excitation(ctx, hb_exc, synth_exc, hb_gain);
01233
01234 hb_synthesis(ctx, sub, &ctx->samples_hb[LP_ORDER_16k],
01235 hb_exc, ctx->isf_cur, ctx->isf_past_final);
01236
01237
01238 hb_fir_filter(hb_samples, bpf_6_7_coef, ctx->bpf_6_7_mem,
01239 &ctx->samples_hb[LP_ORDER_16k]);
01240
01241 if (ctx->fr_cur_mode == MODE_23k85)
01242 hb_fir_filter(hb_samples, lpf_7_coef, ctx->lpf_7_mem,
01243 hb_samples);
01244
01245
01246 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
01247 sub_buf[i] = (sub_buf[i] + hb_samples[i]) * (1.0f / (1 << 15));
01248
01249
01250 update_sub_state(ctx);
01251 }
01252
01253
01254 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(ctx->isp[3][0]));
01255 memcpy(ctx->isf_past_final, ctx->isf_cur, LP_ORDER * sizeof(float));
01256
01257 *got_frame_ptr = 1;
01258 *(AVFrame *)data = ctx->avframe;
01259
01260 return expected_fr_size;
01261 }
01262
01263 AVCodec ff_amrwb_decoder = {
01264 .name = "amrwb",
01265 .type = AVMEDIA_TYPE_AUDIO,
01266 .id = AV_CODEC_ID_AMR_WB,
01267 .priv_data_size = sizeof(AMRWBContext),
01268 .init = amrwb_decode_init,
01269 .decode = amrwb_decode_frame,
01270 .capabilities = CODEC_CAP_DR1,
01271 .long_name = NULL_IF_CONFIG_SMALL("AMR-WB (Adaptive Multi-Rate WideBand)"),
01272 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
01273 AV_SAMPLE_FMT_NONE },
01274 };