00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/lfg.h"
00028
00029 #include "avcodec.h"
00030 #include "lsp.h"
00031 #include "celp_math.h"
00032 #include "celp_filters.h"
00033 #include "acelp_filters.h"
00034 #include "acelp_vectors.h"
00035 #include "acelp_pitch_delay.h"
00036
00037 #define AMR_USE_16BIT_TABLES
00038 #include "amr.h"
00039
00040 #include "amrwbdata.h"
00041
00042 typedef struct {
00043 AVFrame avframe;
00044 AMRWBFrame frame;
00045 enum Mode fr_cur_mode;
00046 uint8_t fr_quality;
00047 float isf_cur[LP_ORDER];
00048 float isf_q_past[LP_ORDER];
00049 float isf_past_final[LP_ORDER];
00050 double isp[4][LP_ORDER];
00051 double isp_sub4_past[LP_ORDER];
00052
00053 float lp_coef[4][LP_ORDER];
00054
00055 uint8_t base_pitch_lag;
00056 uint8_t pitch_lag_int;
00057
00058 float excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 2 + AMRWB_SFR_SIZE];
00059 float *excitation;
00060
00061 float pitch_vector[AMRWB_SFR_SIZE];
00062 float fixed_vector[AMRWB_SFR_SIZE];
00063
00064 float prediction_error[4];
00065 float pitch_gain[6];
00066 float fixed_gain[2];
00067
00068 float tilt_coef;
00069
00070 float prev_sparse_fixed_gain;
00071 uint8_t prev_ir_filter_nr;
00072 float prev_tr_gain;
00073
00074 float samples_az[LP_ORDER + AMRWB_SFR_SIZE];
00075 float samples_up[UPS_MEM_SIZE + AMRWB_SFR_SIZE];
00076 float samples_hb[LP_ORDER_16k + AMRWB_SFR_SIZE_16k];
00077
00078 float hpf_31_mem[2], hpf_400_mem[2];
00079 float demph_mem[1];
00080 float bpf_6_7_mem[HB_FIR_SIZE];
00081 float lpf_7_mem[HB_FIR_SIZE];
00082
00083 AVLFG prng;
00084 uint8_t first_frame;
00085 } AMRWBContext;
00086
00087 static av_cold int amrwb_decode_init(AVCodecContext *avctx)
00088 {
00089 AMRWBContext *ctx = avctx->priv_data;
00090 int i;
00091
00092 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00093
00094 av_lfg_init(&ctx->prng, 1);
00095
00096 ctx->excitation = &ctx->excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 1];
00097 ctx->first_frame = 1;
00098
00099 for (i = 0; i < LP_ORDER; i++)
00100 ctx->isf_past_final[i] = isf_init[i] * (1.0f / (1 << 15));
00101
00102 for (i = 0; i < 4; i++)
00103 ctx->prediction_error[i] = MIN_ENERGY;
00104
00105 avcodec_get_frame_defaults(&ctx->avframe);
00106 avctx->coded_frame = &ctx->avframe;
00107
00108 return 0;
00109 }
00110
00120 static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf)
00121 {
00122
00123 ctx->fr_cur_mode = buf[0] >> 3 & 0x0F;
00124 ctx->fr_quality = (buf[0] & 0x4) != 0x4;
00125
00126 return 1;
00127 }
00128
00136 static void decode_isf_indices_36b(uint16_t *ind, float *isf_q)
00137 {
00138 int i;
00139
00140 for (i = 0; i < 9; i++)
00141 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
00142
00143 for (i = 0; i < 7; i++)
00144 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
00145
00146 for (i = 0; i < 5; i++)
00147 isf_q[i] += dico21_isf_36b[ind[2]][i] * (1.0f / (1 << 15));
00148
00149 for (i = 0; i < 4; i++)
00150 isf_q[i + 5] += dico22_isf_36b[ind[3]][i] * (1.0f / (1 << 15));
00151
00152 for (i = 0; i < 7; i++)
00153 isf_q[i + 9] += dico23_isf_36b[ind[4]][i] * (1.0f / (1 << 15));
00154 }
00155
00163 static void decode_isf_indices_46b(uint16_t *ind, float *isf_q)
00164 {
00165 int i;
00166
00167 for (i = 0; i < 9; i++)
00168 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
00169
00170 for (i = 0; i < 7; i++)
00171 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
00172
00173 for (i = 0; i < 3; i++)
00174 isf_q[i] += dico21_isf[ind[2]][i] * (1.0f / (1 << 15));
00175
00176 for (i = 0; i < 3; i++)
00177 isf_q[i + 3] += dico22_isf[ind[3]][i] * (1.0f / (1 << 15));
00178
00179 for (i = 0; i < 3; i++)
00180 isf_q[i + 6] += dico23_isf[ind[4]][i] * (1.0f / (1 << 15));
00181
00182 for (i = 0; i < 3; i++)
00183 isf_q[i + 9] += dico24_isf[ind[5]][i] * (1.0f / (1 << 15));
00184
00185 for (i = 0; i < 4; i++)
00186 isf_q[i + 12] += dico25_isf[ind[6]][i] * (1.0f / (1 << 15));
00187 }
00188
00197 static void isf_add_mean_and_past(float *isf_q, float *isf_past)
00198 {
00199 int i;
00200 float tmp;
00201
00202 for (i = 0; i < LP_ORDER; i++) {
00203 tmp = isf_q[i];
00204 isf_q[i] += isf_mean[i] * (1.0f / (1 << 15));
00205 isf_q[i] += PRED_FACTOR * isf_past[i];
00206 isf_past[i] = tmp;
00207 }
00208 }
00209
00217 static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past)
00218 {
00219 int i, k;
00220
00221 for (k = 0; k < 3; k++) {
00222 float c = isfp_inter[k];
00223 for (i = 0; i < LP_ORDER; i++)
00224 isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i];
00225 }
00226 }
00227
00239 static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index,
00240 uint8_t *base_lag_int, int subframe)
00241 {
00242 if (subframe == 0 || subframe == 2) {
00243 if (pitch_index < 376) {
00244 *lag_int = (pitch_index + 137) >> 2;
00245 *lag_frac = pitch_index - (*lag_int << 2) + 136;
00246 } else if (pitch_index < 440) {
00247 *lag_int = (pitch_index + 257 - 376) >> 1;
00248 *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) << 1;
00249
00250 } else {
00251 *lag_int = pitch_index - 280;
00252 *lag_frac = 0;
00253 }
00254
00255 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
00256 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15);
00257
00258
00259
00260 } else {
00261 *lag_int = (pitch_index + 1) >> 2;
00262 *lag_frac = pitch_index - (*lag_int << 2);
00263 *lag_int += *base_lag_int;
00264 }
00265 }
00266
00272 static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index,
00273 uint8_t *base_lag_int, int subframe, enum Mode mode)
00274 {
00275 if (subframe == 0 || (subframe == 2 && mode != MODE_6k60)) {
00276 if (pitch_index < 116) {
00277 *lag_int = (pitch_index + 69) >> 1;
00278 *lag_frac = (pitch_index - (*lag_int << 1) + 68) << 1;
00279 } else {
00280 *lag_int = pitch_index - 24;
00281 *lag_frac = 0;
00282 }
00283
00284 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
00285 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15);
00286 } else {
00287 *lag_int = (pitch_index + 1) >> 1;
00288 *lag_frac = (pitch_index - (*lag_int << 1)) << 1;
00289 *lag_int += *base_lag_int;
00290 }
00291 }
00292
00301 static void decode_pitch_vector(AMRWBContext *ctx,
00302 const AMRWBSubFrame *amr_subframe,
00303 const int subframe)
00304 {
00305 int pitch_lag_int, pitch_lag_frac;
00306 int i;
00307 float *exc = ctx->excitation;
00308 enum Mode mode = ctx->fr_cur_mode;
00309
00310 if (mode <= MODE_8k85) {
00311 decode_pitch_lag_low(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
00312 &ctx->base_pitch_lag, subframe, mode);
00313 } else
00314 decode_pitch_lag_high(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
00315 &ctx->base_pitch_lag, subframe);
00316
00317 ctx->pitch_lag_int = pitch_lag_int;
00318 pitch_lag_int += pitch_lag_frac > 0;
00319
00320
00321
00322 ff_acelp_interpolatef(exc, exc + 1 - pitch_lag_int,
00323 ac_inter, 4,
00324 pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4),
00325 LP_ORDER, AMRWB_SFR_SIZE + 1);
00326
00327
00328
00329 if (amr_subframe->ltp) {
00330 memcpy(ctx->pitch_vector, exc, AMRWB_SFR_SIZE * sizeof(float));
00331 } else {
00332 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00333 ctx->pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] +
00334 0.18 * exc[i + 1];
00335 memcpy(exc, ctx->pitch_vector, AMRWB_SFR_SIZE * sizeof(float));
00336 }
00337 }
00338
00340 #define BIT_STR(x,lsb,len) (((x) >> (lsb)) & ((1 << (len)) - 1))
00341
00343 #define BIT_POS(x, p) (((x) >> (p)) & 1)
00344
00358 static inline void decode_1p_track(int *out, int code, int m, int off)
00359 {
00360 int pos = BIT_STR(code, 0, m) + off;
00361
00362 out[0] = BIT_POS(code, m) ? -pos : pos;
00363 }
00364
00365 static inline void decode_2p_track(int *out, int code, int m, int off)
00366 {
00367 int pos0 = BIT_STR(code, m, m) + off;
00368 int pos1 = BIT_STR(code, 0, m) + off;
00369
00370 out[0] = BIT_POS(code, 2*m) ? -pos0 : pos0;
00371 out[1] = BIT_POS(code, 2*m) ? -pos1 : pos1;
00372 out[1] = pos0 > pos1 ? -out[1] : out[1];
00373 }
00374
00375 static void decode_3p_track(int *out, int code, int m, int off)
00376 {
00377 int half_2p = BIT_POS(code, 2*m - 1) << (m - 1);
00378
00379 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
00380 m - 1, off + half_2p);
00381 decode_1p_track(out + 2, BIT_STR(code, 2*m, m + 1), m, off);
00382 }
00383
00384 static void decode_4p_track(int *out, int code, int m, int off)
00385 {
00386 int half_4p, subhalf_2p;
00387 int b_offset = 1 << (m - 1);
00388
00389 switch (BIT_STR(code, 4*m - 2, 2)) {
00390 case 0:
00391 half_4p = BIT_POS(code, 4*m - 3) << (m - 1);
00392 subhalf_2p = BIT_POS(code, 2*m - 3) << (m - 2);
00393
00394 decode_2p_track(out, BIT_STR(code, 0, 2*m - 3),
00395 m - 2, off + half_4p + subhalf_2p);
00396 decode_2p_track(out + 2, BIT_STR(code, 2*m - 2, 2*m - 1),
00397 m - 1, off + half_4p);
00398 break;
00399 case 1:
00400 decode_1p_track(out, BIT_STR(code, 3*m - 2, m),
00401 m - 1, off);
00402 decode_3p_track(out + 1, BIT_STR(code, 0, 3*m - 2),
00403 m - 1, off + b_offset);
00404 break;
00405 case 2:
00406 decode_2p_track(out, BIT_STR(code, 2*m - 1, 2*m - 1),
00407 m - 1, off);
00408 decode_2p_track(out + 2, BIT_STR(code, 0, 2*m - 1),
00409 m - 1, off + b_offset);
00410 break;
00411 case 3:
00412 decode_3p_track(out, BIT_STR(code, m, 3*m - 2),
00413 m - 1, off);
00414 decode_1p_track(out + 3, BIT_STR(code, 0, m),
00415 m - 1, off + b_offset);
00416 break;
00417 }
00418 }
00419
00420 static void decode_5p_track(int *out, int code, int m, int off)
00421 {
00422 int half_3p = BIT_POS(code, 5*m - 1) << (m - 1);
00423
00424 decode_3p_track(out, BIT_STR(code, 2*m + 1, 3*m - 2),
00425 m - 1, off + half_3p);
00426
00427 decode_2p_track(out + 3, BIT_STR(code, 0, 2*m + 1), m, off);
00428 }
00429
00430 static void decode_6p_track(int *out, int code, int m, int off)
00431 {
00432 int b_offset = 1 << (m - 1);
00433
00434 int half_more = BIT_POS(code, 6*m - 5) << (m - 1);
00435 int half_other = b_offset - half_more;
00436
00437 switch (BIT_STR(code, 6*m - 4, 2)) {
00438 case 0:
00439 decode_1p_track(out, BIT_STR(code, 0, m),
00440 m - 1, off + half_more);
00441 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
00442 m - 1, off + half_more);
00443 break;
00444 case 1:
00445 decode_1p_track(out, BIT_STR(code, 0, m),
00446 m - 1, off + half_other);
00447 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
00448 m - 1, off + half_more);
00449 break;
00450 case 2:
00451 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
00452 m - 1, off + half_other);
00453 decode_4p_track(out + 2, BIT_STR(code, 2*m - 1, 4*m - 4),
00454 m - 1, off + half_more);
00455 break;
00456 case 3:
00457 decode_3p_track(out, BIT_STR(code, 3*m - 2, 3*m - 2),
00458 m - 1, off);
00459 decode_3p_track(out + 3, BIT_STR(code, 0, 3*m - 2),
00460 m - 1, off + b_offset);
00461 break;
00462 }
00463 }
00464
00474 static void decode_fixed_vector(float *fixed_vector, const uint16_t *pulse_hi,
00475 const uint16_t *pulse_lo, const enum Mode mode)
00476 {
00477
00478
00479 int sig_pos[4][6];
00480 int spacing = (mode == MODE_6k60) ? 2 : 4;
00481 int i, j;
00482
00483 switch (mode) {
00484 case MODE_6k60:
00485 for (i = 0; i < 2; i++)
00486 decode_1p_track(sig_pos[i], pulse_lo[i], 5, 1);
00487 break;
00488 case MODE_8k85:
00489 for (i = 0; i < 4; i++)
00490 decode_1p_track(sig_pos[i], pulse_lo[i], 4, 1);
00491 break;
00492 case MODE_12k65:
00493 for (i = 0; i < 4; i++)
00494 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
00495 break;
00496 case MODE_14k25:
00497 for (i = 0; i < 2; i++)
00498 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
00499 for (i = 2; i < 4; i++)
00500 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
00501 break;
00502 case MODE_15k85:
00503 for (i = 0; i < 4; i++)
00504 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
00505 break;
00506 case MODE_18k25:
00507 for (i = 0; i < 4; i++)
00508 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
00509 ((int) pulse_hi[i] << 14), 4, 1);
00510 break;
00511 case MODE_19k85:
00512 for (i = 0; i < 2; i++)
00513 decode_5p_track(sig_pos[i], (int) pulse_lo[i] +
00514 ((int) pulse_hi[i] << 10), 4, 1);
00515 for (i = 2; i < 4; i++)
00516 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
00517 ((int) pulse_hi[i] << 14), 4, 1);
00518 break;
00519 case MODE_23k05:
00520 case MODE_23k85:
00521 for (i = 0; i < 4; i++)
00522 decode_6p_track(sig_pos[i], (int) pulse_lo[i] +
00523 ((int) pulse_hi[i] << 11), 4, 1);
00524 break;
00525 }
00526
00527 memset(fixed_vector, 0, sizeof(float) * AMRWB_SFR_SIZE);
00528
00529 for (i = 0; i < 4; i++)
00530 for (j = 0; j < pulses_nb_per_mode_tr[mode][i]; j++) {
00531 int pos = (FFABS(sig_pos[i][j]) - 1) * spacing + i;
00532
00533 fixed_vector[pos] += sig_pos[i][j] < 0 ? -1.0 : 1.0;
00534 }
00535 }
00536
00545 static void decode_gains(const uint8_t vq_gain, const enum Mode mode,
00546 float *fixed_gain_factor, float *pitch_gain)
00547 {
00548 const int16_t *gains = (mode <= MODE_8k85 ? qua_gain_6b[vq_gain] :
00549 qua_gain_7b[vq_gain]);
00550
00551 *pitch_gain = gains[0] * (1.0f / (1 << 14));
00552 *fixed_gain_factor = gains[1] * (1.0f / (1 << 11));
00553 }
00554
00561
00562
00563 static void pitch_sharpening(AMRWBContext *ctx, float *fixed_vector)
00564 {
00565 int i;
00566
00567
00568 for (i = AMRWB_SFR_SIZE - 1; i != 0; i--)
00569 fixed_vector[i] -= fixed_vector[i - 1] * ctx->tilt_coef;
00570
00571
00572 for (i = ctx->pitch_lag_int; i < AMRWB_SFR_SIZE; i++)
00573 fixed_vector[i] += fixed_vector[i - ctx->pitch_lag_int] * 0.85;
00574 }
00575
00582
00583
00584 static float voice_factor(float *p_vector, float p_gain,
00585 float *f_vector, float f_gain)
00586 {
00587 double p_ener = (double) ff_dot_productf(p_vector, p_vector,
00588 AMRWB_SFR_SIZE) * p_gain * p_gain;
00589 double f_ener = (double) ff_dot_productf(f_vector, f_vector,
00590 AMRWB_SFR_SIZE) * f_gain * f_gain;
00591
00592 return (p_ener - f_ener) / (p_ener + f_ener);
00593 }
00594
00605 static float *anti_sparseness(AMRWBContext *ctx,
00606 float *fixed_vector, float *buf)
00607 {
00608 int ir_filter_nr;
00609
00610 if (ctx->fr_cur_mode > MODE_8k85)
00611 return fixed_vector;
00612
00613 if (ctx->pitch_gain[0] < 0.6) {
00614 ir_filter_nr = 0;
00615 } else if (ctx->pitch_gain[0] < 0.9) {
00616 ir_filter_nr = 1;
00617 } else
00618 ir_filter_nr = 2;
00619
00620
00621 if (ctx->fixed_gain[0] > 3.0 * ctx->fixed_gain[1]) {
00622 if (ir_filter_nr < 2)
00623 ir_filter_nr++;
00624 } else {
00625 int i, count = 0;
00626
00627 for (i = 0; i < 6; i++)
00628 if (ctx->pitch_gain[i] < 0.6)
00629 count++;
00630
00631 if (count > 2)
00632 ir_filter_nr = 0;
00633
00634 if (ir_filter_nr > ctx->prev_ir_filter_nr + 1)
00635 ir_filter_nr--;
00636 }
00637
00638
00639 ctx->prev_ir_filter_nr = ir_filter_nr;
00640
00641 ir_filter_nr += (ctx->fr_cur_mode == MODE_8k85);
00642
00643 if (ir_filter_nr < 2) {
00644 int i;
00645 const float *coef = ir_filters_lookup[ir_filter_nr];
00646
00647
00648
00649
00650
00651
00652
00653
00654 memset(buf, 0, sizeof(float) * AMRWB_SFR_SIZE);
00655 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00656 if (fixed_vector[i])
00657 ff_celp_circ_addf(buf, buf, coef, i, fixed_vector[i],
00658 AMRWB_SFR_SIZE);
00659 fixed_vector = buf;
00660 }
00661
00662 return fixed_vector;
00663 }
00664
00669 static float stability_factor(const float *isf, const float *isf_past)
00670 {
00671 int i;
00672 float acc = 0.0;
00673
00674 for (i = 0; i < LP_ORDER - 1; i++)
00675 acc += (isf[i] - isf_past[i]) * (isf[i] - isf_past[i]);
00676
00677
00678
00679 return FFMAX(0.0, 1.25 - acc * 0.8 * 512);
00680 }
00681
00693 static float noise_enhancer(float fixed_gain, float *prev_tr_gain,
00694 float voice_fac, float stab_fac)
00695 {
00696 float sm_fac = 0.5 * (1 - voice_fac) * stab_fac;
00697 float g0;
00698
00699
00700
00701
00702 if (fixed_gain < *prev_tr_gain) {
00703 g0 = FFMIN(*prev_tr_gain, fixed_gain + fixed_gain *
00704 (6226 * (1.0f / (1 << 15))));
00705 } else
00706 g0 = FFMAX(*prev_tr_gain, fixed_gain *
00707 (27536 * (1.0f / (1 << 15))));
00708
00709 *prev_tr_gain = g0;
00710
00711 return sm_fac * g0 + (1 - sm_fac) * fixed_gain;
00712 }
00713
00720 static void pitch_enhancer(float *fixed_vector, float voice_fac)
00721 {
00722 int i;
00723 float cpe = 0.125 * (1 + voice_fac);
00724 float last = fixed_vector[0];
00725
00726 fixed_vector[0] -= cpe * fixed_vector[1];
00727
00728 for (i = 1; i < AMRWB_SFR_SIZE - 1; i++) {
00729 float cur = fixed_vector[i];
00730
00731 fixed_vector[i] -= cpe * (last + fixed_vector[i + 1]);
00732 last = cur;
00733 }
00734
00735 fixed_vector[AMRWB_SFR_SIZE - 1] -= cpe * last;
00736 }
00737
00748 static void synthesis(AMRWBContext *ctx, float *lpc, float *excitation,
00749 float fixed_gain, const float *fixed_vector,
00750 float *samples)
00751 {
00752 ff_weighted_vector_sumf(excitation, ctx->pitch_vector, fixed_vector,
00753 ctx->pitch_gain[0], fixed_gain, AMRWB_SFR_SIZE);
00754
00755
00756 if (ctx->pitch_gain[0] > 0.5 && ctx->fr_cur_mode <= MODE_8k85) {
00757 int i;
00758 float energy = ff_dot_productf(excitation, excitation,
00759 AMRWB_SFR_SIZE);
00760
00761
00762
00763 float pitch_factor = 0.25 * ctx->pitch_gain[0] * ctx->pitch_gain[0];
00764
00765 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00766 excitation[i] += pitch_factor * ctx->pitch_vector[i];
00767
00768 ff_scale_vector_to_given_sum_of_squares(excitation, excitation,
00769 energy, AMRWB_SFR_SIZE);
00770 }
00771
00772 ff_celp_lp_synthesis_filterf(samples, lpc, excitation,
00773 AMRWB_SFR_SIZE, LP_ORDER);
00774 }
00775
00785 static void de_emphasis(float *out, float *in, float m, float mem[1])
00786 {
00787 int i;
00788
00789 out[0] = in[0] + m * mem[0];
00790
00791 for (i = 1; i < AMRWB_SFR_SIZE; i++)
00792 out[i] = in[i] + out[i - 1] * m;
00793
00794 mem[0] = out[AMRWB_SFR_SIZE - 1];
00795 }
00796
00805 static void upsample_5_4(float *out, const float *in, int o_size)
00806 {
00807 const float *in0 = in - UPS_FIR_SIZE + 1;
00808 int i, j, k;
00809 int int_part = 0, frac_part;
00810
00811 i = 0;
00812 for (j = 0; j < o_size / 5; j++) {
00813 out[i] = in[int_part];
00814 frac_part = 4;
00815 i++;
00816
00817 for (k = 1; k < 5; k++) {
00818 out[i] = ff_dot_productf(in0 + int_part, upsample_fir[4 - frac_part],
00819 UPS_MEM_SIZE);
00820 int_part++;
00821 frac_part--;
00822 i++;
00823 }
00824 }
00825 }
00826
00836 static float find_hb_gain(AMRWBContext *ctx, const float *synth,
00837 uint16_t hb_idx, uint8_t vad)
00838 {
00839 int wsp = (vad > 0);
00840 float tilt;
00841
00842 if (ctx->fr_cur_mode == MODE_23k85)
00843 return qua_hb_gain[hb_idx] * (1.0f / (1 << 14));
00844
00845 tilt = ff_dot_productf(synth, synth + 1, AMRWB_SFR_SIZE - 1) /
00846 ff_dot_productf(synth, synth, AMRWB_SFR_SIZE);
00847
00848
00849 return av_clipf((1.0 - FFMAX(0.0, tilt)) * (1.25 - 0.25 * wsp), 0.1, 1.0);
00850 }
00851
00861 static void scaled_hb_excitation(AMRWBContext *ctx, float *hb_exc,
00862 const float *synth_exc, float hb_gain)
00863 {
00864 int i;
00865 float energy = ff_dot_productf(synth_exc, synth_exc, AMRWB_SFR_SIZE);
00866
00867
00868 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
00869 hb_exc[i] = 32768.0 - (uint16_t) av_lfg_get(&ctx->prng);
00870
00871 ff_scale_vector_to_given_sum_of_squares(hb_exc, hb_exc,
00872 energy * hb_gain * hb_gain,
00873 AMRWB_SFR_SIZE_16k);
00874 }
00875
00879 static float auto_correlation(float *diff_isf, float mean, int lag)
00880 {
00881 int i;
00882 float sum = 0.0;
00883
00884 for (i = 7; i < LP_ORDER - 2; i++) {
00885 float prod = (diff_isf[i] - mean) * (diff_isf[i - lag] - mean);
00886 sum += prod * prod;
00887 }
00888 return sum;
00889 }
00890
00898 static void extrapolate_isf(float isf[LP_ORDER_16k])
00899 {
00900 float diff_isf[LP_ORDER - 2], diff_mean;
00901 float *diff_hi = diff_isf - LP_ORDER + 1;
00902 float corr_lag[3];
00903 float est, scale;
00904 int i, i_max_corr;
00905
00906 isf[LP_ORDER_16k - 1] = isf[LP_ORDER - 1];
00907
00908
00909 for (i = 0; i < LP_ORDER - 2; i++)
00910 diff_isf[i] = isf[i + 1] - isf[i];
00911
00912 diff_mean = 0.0;
00913 for (i = 2; i < LP_ORDER - 2; i++)
00914 diff_mean += diff_isf[i] * (1.0f / (LP_ORDER - 4));
00915
00916
00917 i_max_corr = 0;
00918 for (i = 0; i < 3; i++) {
00919 corr_lag[i] = auto_correlation(diff_isf, diff_mean, i + 2);
00920
00921 if (corr_lag[i] > corr_lag[i_max_corr])
00922 i_max_corr = i;
00923 }
00924 i_max_corr++;
00925
00926 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00927 isf[i] = isf[i - 1] + isf[i - 1 - i_max_corr]
00928 - isf[i - 2 - i_max_corr];
00929
00930
00931 est = 7965 + (isf[2] - isf[3] - isf[4]) / 6.0;
00932 scale = 0.5 * (FFMIN(est, 7600) - isf[LP_ORDER - 2]) /
00933 (isf[LP_ORDER_16k - 2] - isf[LP_ORDER - 2]);
00934
00935 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00936 diff_hi[i] = scale * (isf[i] - isf[i - 1]);
00937
00938
00939 for (i = LP_ORDER; i < LP_ORDER_16k - 1; i++)
00940 if (diff_hi[i] + diff_hi[i - 1] < 5.0) {
00941 if (diff_hi[i] > diff_hi[i - 1]) {
00942 diff_hi[i - 1] = 5.0 - diff_hi[i];
00943 } else
00944 diff_hi[i] = 5.0 - diff_hi[i - 1];
00945 }
00946
00947 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00948 isf[i] = isf[i - 1] + diff_hi[i] * (1.0f / (1 << 15));
00949
00950
00951 for (i = 0; i < LP_ORDER_16k - 1; i++)
00952 isf[i] *= 0.8;
00953 }
00954
00964 static void lpc_weighting(float *out, const float *lpc, float gamma, int size)
00965 {
00966 int i;
00967 float fac = gamma;
00968
00969 for (i = 0; i < size; i++) {
00970 out[i] = lpc[i] * fac;
00971 fac *= gamma;
00972 }
00973 }
00974
00986 static void hb_synthesis(AMRWBContext *ctx, int subframe, float *samples,
00987 const float *exc, const float *isf, const float *isf_past)
00988 {
00989 float hb_lpc[LP_ORDER_16k];
00990 enum Mode mode = ctx->fr_cur_mode;
00991
00992 if (mode == MODE_6k60) {
00993 float e_isf[LP_ORDER_16k];
00994 double e_isp[LP_ORDER_16k];
00995
00996 ff_weighted_vector_sumf(e_isf, isf_past, isf, isfp_inter[subframe],
00997 1.0 - isfp_inter[subframe], LP_ORDER);
00998
00999 extrapolate_isf(e_isf);
01000
01001 e_isf[LP_ORDER_16k - 1] *= 2.0;
01002 ff_acelp_lsf2lspd(e_isp, e_isf, LP_ORDER_16k);
01003 ff_amrwb_lsp2lpc(e_isp, hb_lpc, LP_ORDER_16k);
01004
01005 lpc_weighting(hb_lpc, hb_lpc, 0.9, LP_ORDER_16k);
01006 } else {
01007 lpc_weighting(hb_lpc, ctx->lp_coef[subframe], 0.6, LP_ORDER);
01008 }
01009
01010 ff_celp_lp_synthesis_filterf(samples, hb_lpc, exc, AMRWB_SFR_SIZE_16k,
01011 (mode == MODE_6k60) ? LP_ORDER_16k : LP_ORDER);
01012 }
01013
01025 static void hb_fir_filter(float *out, const float fir_coef[HB_FIR_SIZE + 1],
01026 float mem[HB_FIR_SIZE], const float *in)
01027 {
01028 int i, j;
01029 float data[AMRWB_SFR_SIZE_16k + HB_FIR_SIZE];
01030
01031 memcpy(data, mem, HB_FIR_SIZE * sizeof(float));
01032 memcpy(data + HB_FIR_SIZE, in, AMRWB_SFR_SIZE_16k * sizeof(float));
01033
01034 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++) {
01035 out[i] = 0.0;
01036 for (j = 0; j <= HB_FIR_SIZE; j++)
01037 out[i] += data[i + j] * fir_coef[j];
01038 }
01039
01040 memcpy(mem, data + AMRWB_SFR_SIZE_16k, HB_FIR_SIZE * sizeof(float));
01041 }
01042
01046 static void update_sub_state(AMRWBContext *ctx)
01047 {
01048 memmove(&ctx->excitation_buf[0], &ctx->excitation_buf[AMRWB_SFR_SIZE],
01049 (AMRWB_P_DELAY_MAX + LP_ORDER + 1) * sizeof(float));
01050
01051 memmove(&ctx->pitch_gain[1], &ctx->pitch_gain[0], 5 * sizeof(float));
01052 memmove(&ctx->fixed_gain[1], &ctx->fixed_gain[0], 1 * sizeof(float));
01053
01054 memmove(&ctx->samples_az[0], &ctx->samples_az[AMRWB_SFR_SIZE],
01055 LP_ORDER * sizeof(float));
01056 memmove(&ctx->samples_up[0], &ctx->samples_up[AMRWB_SFR_SIZE],
01057 UPS_MEM_SIZE * sizeof(float));
01058 memmove(&ctx->samples_hb[0], &ctx->samples_hb[AMRWB_SFR_SIZE_16k],
01059 LP_ORDER_16k * sizeof(float));
01060 }
01061
01062 static int amrwb_decode_frame(AVCodecContext *avctx, void *data,
01063 int *got_frame_ptr, AVPacket *avpkt)
01064 {
01065 AMRWBContext *ctx = avctx->priv_data;
01066 AMRWBFrame *cf = &ctx->frame;
01067 const uint8_t *buf = avpkt->data;
01068 int buf_size = avpkt->size;
01069 int expected_fr_size, header_size;
01070 float *buf_out;
01071 float spare_vector[AMRWB_SFR_SIZE];
01072 float fixed_gain_factor;
01073 float *synth_fixed_vector;
01074 float synth_fixed_gain;
01075 float voice_fac, stab_fac;
01076 float synth_exc[AMRWB_SFR_SIZE];
01077 float hb_exc[AMRWB_SFR_SIZE_16k];
01078 float hb_samples[AMRWB_SFR_SIZE_16k];
01079 float hb_gain;
01080 int sub, i, ret;
01081
01082
01083 ctx->avframe.nb_samples = 4 * AMRWB_SFR_SIZE_16k;
01084 if ((ret = avctx->get_buffer(avctx, &ctx->avframe)) < 0) {
01085 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01086 return ret;
01087 }
01088 buf_out = (float *)ctx->avframe.data[0];
01089
01090 header_size = decode_mime_header(ctx, buf);
01091 if (ctx->fr_cur_mode > MODE_SID) {
01092 av_log(avctx, AV_LOG_ERROR,
01093 "Invalid mode %d\n", ctx->fr_cur_mode);
01094 return AVERROR_INVALIDDATA;
01095 }
01096 expected_fr_size = ((cf_sizes_wb[ctx->fr_cur_mode] + 7) >> 3) + 1;
01097
01098 if (buf_size < expected_fr_size) {
01099 av_log(avctx, AV_LOG_ERROR,
01100 "Frame too small (%d bytes). Truncated file?\n", buf_size);
01101 *got_frame_ptr = 0;
01102 return AVERROR_INVALIDDATA;
01103 }
01104
01105 if (!ctx->fr_quality || ctx->fr_cur_mode > MODE_SID)
01106 av_log(avctx, AV_LOG_ERROR, "Encountered a bad or corrupted frame\n");
01107
01108 if (ctx->fr_cur_mode == MODE_SID) {
01109 av_log_missing_feature(avctx, "SID mode", 1);
01110 return -1;
01111 }
01112
01113 ff_amr_bit_reorder((uint16_t *) &ctx->frame, sizeof(AMRWBFrame),
01114 buf + header_size, amr_bit_orderings_by_mode[ctx->fr_cur_mode]);
01115
01116
01117 if (ctx->fr_cur_mode == MODE_6k60) {
01118 decode_isf_indices_36b(cf->isp_id, ctx->isf_cur);
01119 } else {
01120 decode_isf_indices_46b(cf->isp_id, ctx->isf_cur);
01121 }
01122
01123 isf_add_mean_and_past(ctx->isf_cur, ctx->isf_q_past);
01124 ff_set_min_dist_lsf(ctx->isf_cur, MIN_ISF_SPACING, LP_ORDER - 1);
01125
01126 stab_fac = stability_factor(ctx->isf_cur, ctx->isf_past_final);
01127
01128 ctx->isf_cur[LP_ORDER - 1] *= 2.0;
01129 ff_acelp_lsf2lspd(ctx->isp[3], ctx->isf_cur, LP_ORDER);
01130
01131
01132 if (ctx->first_frame) {
01133 ctx->first_frame = 0;
01134 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(double));
01135 }
01136 interpolate_isp(ctx->isp, ctx->isp_sub4_past);
01137
01138 for (sub = 0; sub < 4; sub++)
01139 ff_amrwb_lsp2lpc(ctx->isp[sub], ctx->lp_coef[sub], LP_ORDER);
01140
01141 for (sub = 0; sub < 4; sub++) {
01142 const AMRWBSubFrame *cur_subframe = &cf->subframe[sub];
01143 float *sub_buf = buf_out + sub * AMRWB_SFR_SIZE_16k;
01144
01145
01146 decode_pitch_vector(ctx, cur_subframe, sub);
01147
01148 decode_fixed_vector(ctx->fixed_vector, cur_subframe->pul_ih,
01149 cur_subframe->pul_il, ctx->fr_cur_mode);
01150
01151 pitch_sharpening(ctx, ctx->fixed_vector);
01152
01153 decode_gains(cur_subframe->vq_gain, ctx->fr_cur_mode,
01154 &fixed_gain_factor, &ctx->pitch_gain[0]);
01155
01156 ctx->fixed_gain[0] =
01157 ff_amr_set_fixed_gain(fixed_gain_factor,
01158 ff_dot_productf(ctx->fixed_vector, ctx->fixed_vector,
01159 AMRWB_SFR_SIZE) / AMRWB_SFR_SIZE,
01160 ctx->prediction_error,
01161 ENERGY_MEAN, energy_pred_fac);
01162
01163
01164 voice_fac = voice_factor(ctx->pitch_vector, ctx->pitch_gain[0],
01165 ctx->fixed_vector, ctx->fixed_gain[0]);
01166 ctx->tilt_coef = voice_fac * 0.25 + 0.25;
01167
01168
01169 for (i = 0; i < AMRWB_SFR_SIZE; i++) {
01170 ctx->excitation[i] *= ctx->pitch_gain[0];
01171 ctx->excitation[i] += ctx->fixed_gain[0] * ctx->fixed_vector[i];
01172 ctx->excitation[i] = truncf(ctx->excitation[i]);
01173 }
01174
01175
01176 synth_fixed_gain = noise_enhancer(ctx->fixed_gain[0], &ctx->prev_tr_gain,
01177 voice_fac, stab_fac);
01178
01179 synth_fixed_vector = anti_sparseness(ctx, ctx->fixed_vector,
01180 spare_vector);
01181
01182 pitch_enhancer(synth_fixed_vector, voice_fac);
01183
01184 synthesis(ctx, ctx->lp_coef[sub], synth_exc, synth_fixed_gain,
01185 synth_fixed_vector, &ctx->samples_az[LP_ORDER]);
01186
01187
01188 de_emphasis(&ctx->samples_up[UPS_MEM_SIZE],
01189 &ctx->samples_az[LP_ORDER], PREEMPH_FAC, ctx->demph_mem);
01190
01191 ff_acelp_apply_order_2_transfer_function(&ctx->samples_up[UPS_MEM_SIZE],
01192 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_31_poles,
01193 hpf_31_gain, ctx->hpf_31_mem, AMRWB_SFR_SIZE);
01194
01195 upsample_5_4(sub_buf, &ctx->samples_up[UPS_FIR_SIZE],
01196 AMRWB_SFR_SIZE_16k);
01197
01198
01199 ff_acelp_apply_order_2_transfer_function(hb_samples,
01200 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_400_poles,
01201 hpf_400_gain, ctx->hpf_400_mem, AMRWB_SFR_SIZE);
01202
01203 hb_gain = find_hb_gain(ctx, hb_samples,
01204 cur_subframe->hb_gain, cf->vad);
01205
01206 scaled_hb_excitation(ctx, hb_exc, synth_exc, hb_gain);
01207
01208 hb_synthesis(ctx, sub, &ctx->samples_hb[LP_ORDER_16k],
01209 hb_exc, ctx->isf_cur, ctx->isf_past_final);
01210
01211
01212 hb_fir_filter(hb_samples, bpf_6_7_coef, ctx->bpf_6_7_mem,
01213 &ctx->samples_hb[LP_ORDER_16k]);
01214
01215 if (ctx->fr_cur_mode == MODE_23k85)
01216 hb_fir_filter(hb_samples, lpf_7_coef, ctx->lpf_7_mem,
01217 hb_samples);
01218
01219
01220 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
01221 sub_buf[i] = (sub_buf[i] + hb_samples[i]) * (1.0f / (1 << 15));
01222
01223
01224 update_sub_state(ctx);
01225 }
01226
01227
01228 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(ctx->isp[3][0]));
01229 memcpy(ctx->isf_past_final, ctx->isf_cur, LP_ORDER * sizeof(float));
01230
01231 *got_frame_ptr = 1;
01232 *(AVFrame *)data = ctx->avframe;
01233
01234 return expected_fr_size;
01235 }
01236
01237 AVCodec ff_amrwb_decoder = {
01238 .name = "amrwb",
01239 .type = AVMEDIA_TYPE_AUDIO,
01240 .id = CODEC_ID_AMR_WB,
01241 .priv_data_size = sizeof(AMRWBContext),
01242 .init = amrwb_decode_init,
01243 .decode = amrwb_decode_frame,
01244 .capabilities = CODEC_CAP_DR1,
01245 .long_name = NULL_IF_CONFIG_SMALL("Adaptive Multi-Rate WideBand"),
01246 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
01247 AV_SAMPLE_FMT_NONE },
01248 };