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 "get_bits.h"
00031 #include "lsp.h"
00032 #include "celp_math.h"
00033 #include "celp_filters.h"
00034 #include "acelp_filters.h"
00035 #include "acelp_vectors.h"
00036 #include "acelp_pitch_delay.h"
00037
00038 #define AMR_USE_16BIT_TABLES
00039 #include "amr.h"
00040
00041 #include "amrwbdata.h"
00042
00043 typedef struct {
00044 AVFrame avframe;
00045 AMRWBFrame frame;
00046 enum Mode fr_cur_mode;
00047 uint8_t fr_quality;
00048 float isf_cur[LP_ORDER];
00049 float isf_q_past[LP_ORDER];
00050 float isf_past_final[LP_ORDER];
00051 double isp[4][LP_ORDER];
00052 double isp_sub4_past[LP_ORDER];
00053
00054 float lp_coef[4][LP_ORDER];
00055
00056 uint8_t base_pitch_lag;
00057 uint8_t pitch_lag_int;
00058
00059 float excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 2 + AMRWB_SFR_SIZE];
00060 float *excitation;
00061
00062 float pitch_vector[AMRWB_SFR_SIZE];
00063 float fixed_vector[AMRWB_SFR_SIZE];
00064
00065 float prediction_error[4];
00066 float pitch_gain[6];
00067 float fixed_gain[2];
00068
00069 float tilt_coef;
00070
00071 float prev_sparse_fixed_gain;
00072 uint8_t prev_ir_filter_nr;
00073 float prev_tr_gain;
00074
00075 float samples_az[LP_ORDER + AMRWB_SFR_SIZE];
00076 float samples_up[UPS_MEM_SIZE + AMRWB_SFR_SIZE];
00077 float samples_hb[LP_ORDER_16k + AMRWB_SFR_SIZE_16k];
00078
00079 float hpf_31_mem[2], hpf_400_mem[2];
00080 float demph_mem[1];
00081 float bpf_6_7_mem[HB_FIR_SIZE];
00082 float lpf_7_mem[HB_FIR_SIZE];
00083
00084 AVLFG prng;
00085 uint8_t first_frame;
00086 } AMRWBContext;
00087
00088 static av_cold int amrwb_decode_init(AVCodecContext *avctx)
00089 {
00090 AMRWBContext *ctx = avctx->priv_data;
00091 int i;
00092
00093 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00094
00095 av_lfg_init(&ctx->prng, 1);
00096
00097 ctx->excitation = &ctx->excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 1];
00098 ctx->first_frame = 1;
00099
00100 for (i = 0; i < LP_ORDER; i++)
00101 ctx->isf_past_final[i] = isf_init[i] * (1.0f / (1 << 15));
00102
00103 for (i = 0; i < 4; i++)
00104 ctx->prediction_error[i] = MIN_ENERGY;
00105
00106 avcodec_get_frame_defaults(&ctx->avframe);
00107 avctx->coded_frame = &ctx->avframe;
00108
00109 return 0;
00110 }
00111
00121 static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf)
00122 {
00123 GetBitContext gb;
00124 init_get_bits(&gb, buf, 8);
00125
00126
00127 skip_bits(&gb, 1);
00128 ctx->fr_cur_mode = get_bits(&gb, 4);
00129 ctx->fr_quality = get_bits1(&gb);
00130 skip_bits(&gb, 2);
00131
00132 return 1;
00133 }
00134
00142 static void decode_isf_indices_36b(uint16_t *ind, float *isf_q)
00143 {
00144 int i;
00145
00146 for (i = 0; i < 9; i++)
00147 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
00148
00149 for (i = 0; i < 7; i++)
00150 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
00151
00152 for (i = 0; i < 5; i++)
00153 isf_q[i] += dico21_isf_36b[ind[2]][i] * (1.0f / (1 << 15));
00154
00155 for (i = 0; i < 4; i++)
00156 isf_q[i + 5] += dico22_isf_36b[ind[3]][i] * (1.0f / (1 << 15));
00157
00158 for (i = 0; i < 7; i++)
00159 isf_q[i + 9] += dico23_isf_36b[ind[4]][i] * (1.0f / (1 << 15));
00160 }
00161
00169 static void decode_isf_indices_46b(uint16_t *ind, float *isf_q)
00170 {
00171 int i;
00172
00173 for (i = 0; i < 9; i++)
00174 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
00175
00176 for (i = 0; i < 7; i++)
00177 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
00178
00179 for (i = 0; i < 3; i++)
00180 isf_q[i] += dico21_isf[ind[2]][i] * (1.0f / (1 << 15));
00181
00182 for (i = 0; i < 3; i++)
00183 isf_q[i + 3] += dico22_isf[ind[3]][i] * (1.0f / (1 << 15));
00184
00185 for (i = 0; i < 3; i++)
00186 isf_q[i + 6] += dico23_isf[ind[4]][i] * (1.0f / (1 << 15));
00187
00188 for (i = 0; i < 3; i++)
00189 isf_q[i + 9] += dico24_isf[ind[5]][i] * (1.0f / (1 << 15));
00190
00191 for (i = 0; i < 4; i++)
00192 isf_q[i + 12] += dico25_isf[ind[6]][i] * (1.0f / (1 << 15));
00193 }
00194
00203 static void isf_add_mean_and_past(float *isf_q, float *isf_past)
00204 {
00205 int i;
00206 float tmp;
00207
00208 for (i = 0; i < LP_ORDER; i++) {
00209 tmp = isf_q[i];
00210 isf_q[i] += isf_mean[i] * (1.0f / (1 << 15));
00211 isf_q[i] += PRED_FACTOR * isf_past[i];
00212 isf_past[i] = tmp;
00213 }
00214 }
00215
00223 static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past)
00224 {
00225 int i, k;
00226
00227 for (k = 0; k < 3; k++) {
00228 float c = isfp_inter[k];
00229 for (i = 0; i < LP_ORDER; i++)
00230 isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i];
00231 }
00232 }
00233
00245 static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index,
00246 uint8_t *base_lag_int, int subframe)
00247 {
00248 if (subframe == 0 || subframe == 2) {
00249 if (pitch_index < 376) {
00250 *lag_int = (pitch_index + 137) >> 2;
00251 *lag_frac = pitch_index - (*lag_int << 2) + 136;
00252 } else if (pitch_index < 440) {
00253 *lag_int = (pitch_index + 257 - 376) >> 1;
00254 *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) << 1;
00255
00256 } else {
00257 *lag_int = pitch_index - 280;
00258 *lag_frac = 0;
00259 }
00260
00261 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
00262 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15);
00263
00264
00265
00266 } else {
00267 *lag_int = (pitch_index + 1) >> 2;
00268 *lag_frac = pitch_index - (*lag_int << 2);
00269 *lag_int += *base_lag_int;
00270 }
00271 }
00272
00278 static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index,
00279 uint8_t *base_lag_int, int subframe, enum Mode mode)
00280 {
00281 if (subframe == 0 || (subframe == 2 && mode != MODE_6k60)) {
00282 if (pitch_index < 116) {
00283 *lag_int = (pitch_index + 69) >> 1;
00284 *lag_frac = (pitch_index - (*lag_int << 1) + 68) << 1;
00285 } else {
00286 *lag_int = pitch_index - 24;
00287 *lag_frac = 0;
00288 }
00289
00290 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
00291 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15);
00292 } else {
00293 *lag_int = (pitch_index + 1) >> 1;
00294 *lag_frac = (pitch_index - (*lag_int << 1)) << 1;
00295 *lag_int += *base_lag_int;
00296 }
00297 }
00298
00307 static void decode_pitch_vector(AMRWBContext *ctx,
00308 const AMRWBSubFrame *amr_subframe,
00309 const int subframe)
00310 {
00311 int pitch_lag_int, pitch_lag_frac;
00312 int i;
00313 float *exc = ctx->excitation;
00314 enum Mode mode = ctx->fr_cur_mode;
00315
00316 if (mode <= MODE_8k85) {
00317 decode_pitch_lag_low(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
00318 &ctx->base_pitch_lag, subframe, mode);
00319 } else
00320 decode_pitch_lag_high(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
00321 &ctx->base_pitch_lag, subframe);
00322
00323 ctx->pitch_lag_int = pitch_lag_int;
00324 pitch_lag_int += pitch_lag_frac > 0;
00325
00326
00327
00328 ff_acelp_interpolatef(exc, exc + 1 - pitch_lag_int,
00329 ac_inter, 4,
00330 pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4),
00331 LP_ORDER, AMRWB_SFR_SIZE + 1);
00332
00333
00334
00335 if (amr_subframe->ltp) {
00336 memcpy(ctx->pitch_vector, exc, AMRWB_SFR_SIZE * sizeof(float));
00337 } else {
00338 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00339 ctx->pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] +
00340 0.18 * exc[i + 1];
00341 memcpy(exc, ctx->pitch_vector, AMRWB_SFR_SIZE * sizeof(float));
00342 }
00343 }
00344
00346 #define BIT_STR(x,lsb,len) (((x) >> (lsb)) & ((1 << (len)) - 1))
00347
00349 #define BIT_POS(x, p) (((x) >> (p)) & 1)
00350
00364 static inline void decode_1p_track(int *out, int code, int m, int off)
00365 {
00366 int pos = BIT_STR(code, 0, m) + off;
00367
00368 out[0] = BIT_POS(code, m) ? -pos : pos;
00369 }
00370
00371 static inline void decode_2p_track(int *out, int code, int m, int off)
00372 {
00373 int pos0 = BIT_STR(code, m, m) + off;
00374 int pos1 = BIT_STR(code, 0, m) + off;
00375
00376 out[0] = BIT_POS(code, 2*m) ? -pos0 : pos0;
00377 out[1] = BIT_POS(code, 2*m) ? -pos1 : pos1;
00378 out[1] = pos0 > pos1 ? -out[1] : out[1];
00379 }
00380
00381 static void decode_3p_track(int *out, int code, int m, int off)
00382 {
00383 int half_2p = BIT_POS(code, 2*m - 1) << (m - 1);
00384
00385 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
00386 m - 1, off + half_2p);
00387 decode_1p_track(out + 2, BIT_STR(code, 2*m, m + 1), m, off);
00388 }
00389
00390 static void decode_4p_track(int *out, int code, int m, int off)
00391 {
00392 int half_4p, subhalf_2p;
00393 int b_offset = 1 << (m - 1);
00394
00395 switch (BIT_STR(code, 4*m - 2, 2)) {
00396 case 0:
00397 half_4p = BIT_POS(code, 4*m - 3) << (m - 1);
00398 subhalf_2p = BIT_POS(code, 2*m - 3) << (m - 2);
00399
00400 decode_2p_track(out, BIT_STR(code, 0, 2*m - 3),
00401 m - 2, off + half_4p + subhalf_2p);
00402 decode_2p_track(out + 2, BIT_STR(code, 2*m - 2, 2*m - 1),
00403 m - 1, off + half_4p);
00404 break;
00405 case 1:
00406 decode_1p_track(out, BIT_STR(code, 3*m - 2, m),
00407 m - 1, off);
00408 decode_3p_track(out + 1, BIT_STR(code, 0, 3*m - 2),
00409 m - 1, off + b_offset);
00410 break;
00411 case 2:
00412 decode_2p_track(out, BIT_STR(code, 2*m - 1, 2*m - 1),
00413 m - 1, off);
00414 decode_2p_track(out + 2, BIT_STR(code, 0, 2*m - 1),
00415 m - 1, off + b_offset);
00416 break;
00417 case 3:
00418 decode_3p_track(out, BIT_STR(code, m, 3*m - 2),
00419 m - 1, off);
00420 decode_1p_track(out + 3, BIT_STR(code, 0, m),
00421 m - 1, off + b_offset);
00422 break;
00423 }
00424 }
00425
00426 static void decode_5p_track(int *out, int code, int m, int off)
00427 {
00428 int half_3p = BIT_POS(code, 5*m - 1) << (m - 1);
00429
00430 decode_3p_track(out, BIT_STR(code, 2*m + 1, 3*m - 2),
00431 m - 1, off + half_3p);
00432
00433 decode_2p_track(out + 3, BIT_STR(code, 0, 2*m + 1), m, off);
00434 }
00435
00436 static void decode_6p_track(int *out, int code, int m, int off)
00437 {
00438 int b_offset = 1 << (m - 1);
00439
00440 int half_more = BIT_POS(code, 6*m - 5) << (m - 1);
00441 int half_other = b_offset - half_more;
00442
00443 switch (BIT_STR(code, 6*m - 4, 2)) {
00444 case 0:
00445 decode_1p_track(out, BIT_STR(code, 0, m),
00446 m - 1, off + half_more);
00447 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
00448 m - 1, off + half_more);
00449 break;
00450 case 1:
00451 decode_1p_track(out, BIT_STR(code, 0, m),
00452 m - 1, off + half_other);
00453 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
00454 m - 1, off + half_more);
00455 break;
00456 case 2:
00457 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
00458 m - 1, off + half_other);
00459 decode_4p_track(out + 2, BIT_STR(code, 2*m - 1, 4*m - 4),
00460 m - 1, off + half_more);
00461 break;
00462 case 3:
00463 decode_3p_track(out, BIT_STR(code, 3*m - 2, 3*m - 2),
00464 m - 1, off);
00465 decode_3p_track(out + 3, BIT_STR(code, 0, 3*m - 2),
00466 m - 1, off + b_offset);
00467 break;
00468 }
00469 }
00470
00480 static void decode_fixed_vector(float *fixed_vector, const uint16_t *pulse_hi,
00481 const uint16_t *pulse_lo, const enum Mode mode)
00482 {
00483
00484
00485 int sig_pos[4][6];
00486 int spacing = (mode == MODE_6k60) ? 2 : 4;
00487 int i, j;
00488
00489 switch (mode) {
00490 case MODE_6k60:
00491 for (i = 0; i < 2; i++)
00492 decode_1p_track(sig_pos[i], pulse_lo[i], 5, 1);
00493 break;
00494 case MODE_8k85:
00495 for (i = 0; i < 4; i++)
00496 decode_1p_track(sig_pos[i], pulse_lo[i], 4, 1);
00497 break;
00498 case MODE_12k65:
00499 for (i = 0; i < 4; i++)
00500 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
00501 break;
00502 case MODE_14k25:
00503 for (i = 0; i < 2; i++)
00504 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
00505 for (i = 2; i < 4; i++)
00506 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
00507 break;
00508 case MODE_15k85:
00509 for (i = 0; i < 4; i++)
00510 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
00511 break;
00512 case MODE_18k25:
00513 for (i = 0; i < 4; i++)
00514 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
00515 ((int) pulse_hi[i] << 14), 4, 1);
00516 break;
00517 case MODE_19k85:
00518 for (i = 0; i < 2; i++)
00519 decode_5p_track(sig_pos[i], (int) pulse_lo[i] +
00520 ((int) pulse_hi[i] << 10), 4, 1);
00521 for (i = 2; 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_23k05:
00526 case MODE_23k85:
00527 for (i = 0; i < 4; i++)
00528 decode_6p_track(sig_pos[i], (int) pulse_lo[i] +
00529 ((int) pulse_hi[i] << 11), 4, 1);
00530 break;
00531 }
00532
00533 memset(fixed_vector, 0, sizeof(float) * AMRWB_SFR_SIZE);
00534
00535 for (i = 0; i < 4; i++)
00536 for (j = 0; j < pulses_nb_per_mode_tr[mode][i]; j++) {
00537 int pos = (FFABS(sig_pos[i][j]) - 1) * spacing + i;
00538
00539 fixed_vector[pos] += sig_pos[i][j] < 0 ? -1.0 : 1.0;
00540 }
00541 }
00542
00551 static void decode_gains(const uint8_t vq_gain, const enum Mode mode,
00552 float *fixed_gain_factor, float *pitch_gain)
00553 {
00554 const int16_t *gains = (mode <= MODE_8k85 ? qua_gain_6b[vq_gain] :
00555 qua_gain_7b[vq_gain]);
00556
00557 *pitch_gain = gains[0] * (1.0f / (1 << 14));
00558 *fixed_gain_factor = gains[1] * (1.0f / (1 << 11));
00559 }
00560
00567
00568
00569 static void pitch_sharpening(AMRWBContext *ctx, float *fixed_vector)
00570 {
00571 int i;
00572
00573
00574 for (i = AMRWB_SFR_SIZE - 1; i != 0; i--)
00575 fixed_vector[i] -= fixed_vector[i - 1] * ctx->tilt_coef;
00576
00577
00578 for (i = ctx->pitch_lag_int; i < AMRWB_SFR_SIZE; i++)
00579 fixed_vector[i] += fixed_vector[i - ctx->pitch_lag_int] * 0.85;
00580 }
00581
00588
00589
00590 static float voice_factor(float *p_vector, float p_gain,
00591 float *f_vector, float f_gain)
00592 {
00593 double p_ener = (double) ff_dot_productf(p_vector, p_vector,
00594 AMRWB_SFR_SIZE) * p_gain * p_gain;
00595 double f_ener = (double) ff_dot_productf(f_vector, f_vector,
00596 AMRWB_SFR_SIZE) * f_gain * f_gain;
00597
00598 return (p_ener - f_ener) / (p_ener + f_ener);
00599 }
00600
00611 static float *anti_sparseness(AMRWBContext *ctx,
00612 float *fixed_vector, float *buf)
00613 {
00614 int ir_filter_nr;
00615
00616 if (ctx->fr_cur_mode > MODE_8k85)
00617 return fixed_vector;
00618
00619 if (ctx->pitch_gain[0] < 0.6) {
00620 ir_filter_nr = 0;
00621 } else if (ctx->pitch_gain[0] < 0.9) {
00622 ir_filter_nr = 1;
00623 } else
00624 ir_filter_nr = 2;
00625
00626
00627 if (ctx->fixed_gain[0] > 3.0 * ctx->fixed_gain[1]) {
00628 if (ir_filter_nr < 2)
00629 ir_filter_nr++;
00630 } else {
00631 int i, count = 0;
00632
00633 for (i = 0; i < 6; i++)
00634 if (ctx->pitch_gain[i] < 0.6)
00635 count++;
00636
00637 if (count > 2)
00638 ir_filter_nr = 0;
00639
00640 if (ir_filter_nr > ctx->prev_ir_filter_nr + 1)
00641 ir_filter_nr--;
00642 }
00643
00644
00645 ctx->prev_ir_filter_nr = ir_filter_nr;
00646
00647 ir_filter_nr += (ctx->fr_cur_mode == MODE_8k85);
00648
00649 if (ir_filter_nr < 2) {
00650 int i;
00651 const float *coef = ir_filters_lookup[ir_filter_nr];
00652
00653
00654
00655
00656
00657
00658
00659
00660 memset(buf, 0, sizeof(float) * AMRWB_SFR_SIZE);
00661 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00662 if (fixed_vector[i])
00663 ff_celp_circ_addf(buf, buf, coef, i, fixed_vector[i],
00664 AMRWB_SFR_SIZE);
00665 fixed_vector = buf;
00666 }
00667
00668 return fixed_vector;
00669 }
00670
00675 static float stability_factor(const float *isf, const float *isf_past)
00676 {
00677 int i;
00678 float acc = 0.0;
00679
00680 for (i = 0; i < LP_ORDER - 1; i++)
00681 acc += (isf[i] - isf_past[i]) * (isf[i] - isf_past[i]);
00682
00683
00684
00685 return FFMAX(0.0, 1.25 - acc * 0.8 * 512);
00686 }
00687
00699 static float noise_enhancer(float fixed_gain, float *prev_tr_gain,
00700 float voice_fac, float stab_fac)
00701 {
00702 float sm_fac = 0.5 * (1 - voice_fac) * stab_fac;
00703 float g0;
00704
00705
00706
00707
00708 if (fixed_gain < *prev_tr_gain) {
00709 g0 = FFMIN(*prev_tr_gain, fixed_gain + fixed_gain *
00710 (6226 * (1.0f / (1 << 15))));
00711 } else
00712 g0 = FFMAX(*prev_tr_gain, fixed_gain *
00713 (27536 * (1.0f / (1 << 15))));
00714
00715 *prev_tr_gain = g0;
00716
00717 return sm_fac * g0 + (1 - sm_fac) * fixed_gain;
00718 }
00719
00726 static void pitch_enhancer(float *fixed_vector, float voice_fac)
00727 {
00728 int i;
00729 float cpe = 0.125 * (1 + voice_fac);
00730 float last = fixed_vector[0];
00731
00732 fixed_vector[0] -= cpe * fixed_vector[1];
00733
00734 for (i = 1; i < AMRWB_SFR_SIZE - 1; i++) {
00735 float cur = fixed_vector[i];
00736
00737 fixed_vector[i] -= cpe * (last + fixed_vector[i + 1]);
00738 last = cur;
00739 }
00740
00741 fixed_vector[AMRWB_SFR_SIZE - 1] -= cpe * last;
00742 }
00743
00754 static void synthesis(AMRWBContext *ctx, float *lpc, float *excitation,
00755 float fixed_gain, const float *fixed_vector,
00756 float *samples)
00757 {
00758 ff_weighted_vector_sumf(excitation, ctx->pitch_vector, fixed_vector,
00759 ctx->pitch_gain[0], fixed_gain, AMRWB_SFR_SIZE);
00760
00761
00762 if (ctx->pitch_gain[0] > 0.5 && ctx->fr_cur_mode <= MODE_8k85) {
00763 int i;
00764 float energy = ff_dot_productf(excitation, excitation,
00765 AMRWB_SFR_SIZE);
00766
00767
00768
00769 float pitch_factor = 0.25 * ctx->pitch_gain[0] * ctx->pitch_gain[0];
00770
00771 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00772 excitation[i] += pitch_factor * ctx->pitch_vector[i];
00773
00774 ff_scale_vector_to_given_sum_of_squares(excitation, excitation,
00775 energy, AMRWB_SFR_SIZE);
00776 }
00777
00778 ff_celp_lp_synthesis_filterf(samples, lpc, excitation,
00779 AMRWB_SFR_SIZE, LP_ORDER);
00780 }
00781
00791 static void de_emphasis(float *out, float *in, float m, float mem[1])
00792 {
00793 int i;
00794
00795 out[0] = in[0] + m * mem[0];
00796
00797 for (i = 1; i < AMRWB_SFR_SIZE; i++)
00798 out[i] = in[i] + out[i - 1] * m;
00799
00800 mem[0] = out[AMRWB_SFR_SIZE - 1];
00801 }
00802
00811 static void upsample_5_4(float *out, const float *in, int o_size)
00812 {
00813 const float *in0 = in - UPS_FIR_SIZE + 1;
00814 int i, j, k;
00815 int int_part = 0, frac_part;
00816
00817 i = 0;
00818 for (j = 0; j < o_size / 5; j++) {
00819 out[i] = in[int_part];
00820 frac_part = 4;
00821 i++;
00822
00823 for (k = 1; k < 5; k++) {
00824 out[i] = ff_dot_productf(in0 + int_part, upsample_fir[4 - frac_part],
00825 UPS_MEM_SIZE);
00826 int_part++;
00827 frac_part--;
00828 i++;
00829 }
00830 }
00831 }
00832
00842 static float find_hb_gain(AMRWBContext *ctx, const float *synth,
00843 uint16_t hb_idx, uint8_t vad)
00844 {
00845 int wsp = (vad > 0);
00846 float tilt;
00847
00848 if (ctx->fr_cur_mode == MODE_23k85)
00849 return qua_hb_gain[hb_idx] * (1.0f / (1 << 14));
00850
00851 tilt = ff_dot_productf(synth, synth + 1, AMRWB_SFR_SIZE - 1) /
00852 ff_dot_productf(synth, synth, AMRWB_SFR_SIZE);
00853
00854
00855 return av_clipf((1.0 - FFMAX(0.0, tilt)) * (1.25 - 0.25 * wsp), 0.1, 1.0);
00856 }
00857
00867 static void scaled_hb_excitation(AMRWBContext *ctx, float *hb_exc,
00868 const float *synth_exc, float hb_gain)
00869 {
00870 int i;
00871 float energy = ff_dot_productf(synth_exc, synth_exc, AMRWB_SFR_SIZE);
00872
00873
00874 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
00875 hb_exc[i] = 32768.0 - (uint16_t) av_lfg_get(&ctx->prng);
00876
00877 ff_scale_vector_to_given_sum_of_squares(hb_exc, hb_exc,
00878 energy * hb_gain * hb_gain,
00879 AMRWB_SFR_SIZE_16k);
00880 }
00881
00885 static float auto_correlation(float *diff_isf, float mean, int lag)
00886 {
00887 int i;
00888 float sum = 0.0;
00889
00890 for (i = 7; i < LP_ORDER - 2; i++) {
00891 float prod = (diff_isf[i] - mean) * (diff_isf[i - lag] - mean);
00892 sum += prod * prod;
00893 }
00894 return sum;
00895 }
00896
00904 static void extrapolate_isf(float out[LP_ORDER_16k], float isf[LP_ORDER])
00905 {
00906 float diff_isf[LP_ORDER - 2], diff_mean;
00907 float *diff_hi = diff_isf - LP_ORDER + 1;
00908 float corr_lag[3];
00909 float est, scale;
00910 int i, i_max_corr;
00911
00912 memcpy(out, isf, (LP_ORDER - 1) * sizeof(float));
00913 out[LP_ORDER_16k - 1] = isf[LP_ORDER - 1];
00914
00915
00916 for (i = 0; i < LP_ORDER - 2; i++)
00917 diff_isf[i] = isf[i + 1] - isf[i];
00918
00919 diff_mean = 0.0;
00920 for (i = 2; i < LP_ORDER - 2; i++)
00921 diff_mean += diff_isf[i] * (1.0f / (LP_ORDER - 4));
00922
00923
00924 i_max_corr = 0;
00925 for (i = 0; i < 3; i++) {
00926 corr_lag[i] = auto_correlation(diff_isf, diff_mean, i + 2);
00927
00928 if (corr_lag[i] > corr_lag[i_max_corr])
00929 i_max_corr = i;
00930 }
00931 i_max_corr++;
00932
00933 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00934 out[i] = isf[i - 1] + isf[i - 1 - i_max_corr]
00935 - isf[i - 2 - i_max_corr];
00936
00937
00938 est = 7965 + (out[2] - out[3] - out[4]) / 6.0;
00939 scale = 0.5 * (FFMIN(est, 7600) - out[LP_ORDER - 2]) /
00940 (out[LP_ORDER_16k - 2] - out[LP_ORDER - 2]);
00941
00942 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00943 diff_hi[i] = scale * (out[i] - out[i - 1]);
00944
00945
00946 for (i = LP_ORDER; i < LP_ORDER_16k - 1; i++)
00947 if (diff_hi[i] + diff_hi[i - 1] < 5.0) {
00948 if (diff_hi[i] > diff_hi[i - 1]) {
00949 diff_hi[i - 1] = 5.0 - diff_hi[i];
00950 } else
00951 diff_hi[i] = 5.0 - diff_hi[i - 1];
00952 }
00953
00954 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00955 out[i] = out[i - 1] + diff_hi[i] * (1.0f / (1 << 15));
00956
00957
00958 for (i = 0; i < LP_ORDER_16k - 1; i++)
00959 out[i] *= 0.8;
00960 }
00961
00971 static void lpc_weighting(float *out, const float *lpc, float gamma, int size)
00972 {
00973 int i;
00974 float fac = gamma;
00975
00976 for (i = 0; i < size; i++) {
00977 out[i] = lpc[i] * fac;
00978 fac *= gamma;
00979 }
00980 }
00981
00993 static void hb_synthesis(AMRWBContext *ctx, int subframe, float *samples,
00994 const float *exc, const float *isf, const float *isf_past)
00995 {
00996 float hb_lpc[LP_ORDER_16k];
00997 enum Mode mode = ctx->fr_cur_mode;
00998
00999 if (mode == MODE_6k60) {
01000 float e_isf[LP_ORDER_16k];
01001 double e_isp[LP_ORDER_16k];
01002
01003 ff_weighted_vector_sumf(e_isf, isf_past, isf, isfp_inter[subframe],
01004 1.0 - isfp_inter[subframe], LP_ORDER);
01005
01006 extrapolate_isf(e_isf, e_isf);
01007
01008 e_isf[LP_ORDER_16k - 1] *= 2.0;
01009 ff_acelp_lsf2lspd(e_isp, e_isf, LP_ORDER_16k);
01010 ff_amrwb_lsp2lpc(e_isp, hb_lpc, LP_ORDER_16k);
01011
01012 lpc_weighting(hb_lpc, hb_lpc, 0.9, LP_ORDER_16k);
01013 } else {
01014 lpc_weighting(hb_lpc, ctx->lp_coef[subframe], 0.6, LP_ORDER);
01015 }
01016
01017 ff_celp_lp_synthesis_filterf(samples, hb_lpc, exc, AMRWB_SFR_SIZE_16k,
01018 (mode == MODE_6k60) ? LP_ORDER_16k : LP_ORDER);
01019 }
01020
01032 static void hb_fir_filter(float *out, const float fir_coef[HB_FIR_SIZE + 1],
01033 float mem[HB_FIR_SIZE], const float *in)
01034 {
01035 int i, j;
01036 float data[AMRWB_SFR_SIZE_16k + HB_FIR_SIZE];
01037
01038 memcpy(data, mem, HB_FIR_SIZE * sizeof(float));
01039 memcpy(data + HB_FIR_SIZE, in, AMRWB_SFR_SIZE_16k * sizeof(float));
01040
01041 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++) {
01042 out[i] = 0.0;
01043 for (j = 0; j <= HB_FIR_SIZE; j++)
01044 out[i] += data[i + j] * fir_coef[j];
01045 }
01046
01047 memcpy(mem, data + AMRWB_SFR_SIZE_16k, HB_FIR_SIZE * sizeof(float));
01048 }
01049
01053 static void update_sub_state(AMRWBContext *ctx)
01054 {
01055 memmove(&ctx->excitation_buf[0], &ctx->excitation_buf[AMRWB_SFR_SIZE],
01056 (AMRWB_P_DELAY_MAX + LP_ORDER + 1) * sizeof(float));
01057
01058 memmove(&ctx->pitch_gain[1], &ctx->pitch_gain[0], 5 * sizeof(float));
01059 memmove(&ctx->fixed_gain[1], &ctx->fixed_gain[0], 1 * sizeof(float));
01060
01061 memmove(&ctx->samples_az[0], &ctx->samples_az[AMRWB_SFR_SIZE],
01062 LP_ORDER * sizeof(float));
01063 memmove(&ctx->samples_up[0], &ctx->samples_up[AMRWB_SFR_SIZE],
01064 UPS_MEM_SIZE * sizeof(float));
01065 memmove(&ctx->samples_hb[0], &ctx->samples_hb[AMRWB_SFR_SIZE_16k],
01066 LP_ORDER_16k * sizeof(float));
01067 }
01068
01069 static int amrwb_decode_frame(AVCodecContext *avctx, void *data,
01070 int *got_frame_ptr, AVPacket *avpkt)
01071 {
01072 AMRWBContext *ctx = avctx->priv_data;
01073 AMRWBFrame *cf = &ctx->frame;
01074 const uint8_t *buf = avpkt->data;
01075 int buf_size = avpkt->size;
01076 int expected_fr_size, header_size;
01077 float *buf_out;
01078 float spare_vector[AMRWB_SFR_SIZE];
01079 float fixed_gain_factor;
01080 float *synth_fixed_vector;
01081 float synth_fixed_gain;
01082 float voice_fac, stab_fac;
01083 float synth_exc[AMRWB_SFR_SIZE];
01084 float hb_exc[AMRWB_SFR_SIZE_16k];
01085 float hb_samples[AMRWB_SFR_SIZE_16k];
01086 float hb_gain;
01087 int sub, i, ret;
01088
01089
01090 ctx->avframe.nb_samples = 4 * AMRWB_SFR_SIZE_16k;
01091 if ((ret = avctx->get_buffer(avctx, &ctx->avframe)) < 0) {
01092 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01093 return ret;
01094 }
01095 buf_out = (float *)ctx->avframe.data[0];
01096
01097 header_size = decode_mime_header(ctx, buf);
01098 expected_fr_size = ((cf_sizes_wb[ctx->fr_cur_mode] + 7) >> 3) + 1;
01099
01100 if (buf_size < expected_fr_size) {
01101 av_log(avctx, AV_LOG_ERROR,
01102 "Frame too small (%d bytes). Truncated file?\n", buf_size);
01103 *got_frame_ptr = 0;
01104 return buf_size;
01105 }
01106
01107 if (!ctx->fr_quality || ctx->fr_cur_mode > MODE_SID)
01108 av_log(avctx, AV_LOG_ERROR, "Encountered a bad or corrupted frame\n");
01109
01110 if (ctx->fr_cur_mode == MODE_SID)
01111 av_log_missing_feature(avctx, "SID mode", 1);
01112
01113 if (ctx->fr_cur_mode >= MODE_SID)
01114 return -1;
01115
01116 ff_amr_bit_reorder((uint16_t *) &ctx->frame, sizeof(AMRWBFrame),
01117 buf + header_size, amr_bit_orderings_by_mode[ctx->fr_cur_mode]);
01118
01119
01120 if (ctx->fr_cur_mode == MODE_6k60) {
01121 decode_isf_indices_36b(cf->isp_id, ctx->isf_cur);
01122 } else {
01123 decode_isf_indices_46b(cf->isp_id, ctx->isf_cur);
01124 }
01125
01126 isf_add_mean_and_past(ctx->isf_cur, ctx->isf_q_past);
01127 ff_set_min_dist_lsf(ctx->isf_cur, MIN_ISF_SPACING, LP_ORDER - 1);
01128
01129 stab_fac = stability_factor(ctx->isf_cur, ctx->isf_past_final);
01130
01131 ctx->isf_cur[LP_ORDER - 1] *= 2.0;
01132 ff_acelp_lsf2lspd(ctx->isp[3], ctx->isf_cur, LP_ORDER);
01133
01134
01135 if (ctx->first_frame) {
01136 ctx->first_frame = 0;
01137 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(double));
01138 }
01139 interpolate_isp(ctx->isp, ctx->isp_sub4_past);
01140
01141 for (sub = 0; sub < 4; sub++)
01142 ff_amrwb_lsp2lpc(ctx->isp[sub], ctx->lp_coef[sub], LP_ORDER);
01143
01144 for (sub = 0; sub < 4; sub++) {
01145 const AMRWBSubFrame *cur_subframe = &cf->subframe[sub];
01146 float *sub_buf = buf_out + sub * AMRWB_SFR_SIZE_16k;
01147
01148
01149 decode_pitch_vector(ctx, cur_subframe, sub);
01150
01151 decode_fixed_vector(ctx->fixed_vector, cur_subframe->pul_ih,
01152 cur_subframe->pul_il, ctx->fr_cur_mode);
01153
01154 pitch_sharpening(ctx, ctx->fixed_vector);
01155
01156 decode_gains(cur_subframe->vq_gain, ctx->fr_cur_mode,
01157 &fixed_gain_factor, &ctx->pitch_gain[0]);
01158
01159 ctx->fixed_gain[0] =
01160 ff_amr_set_fixed_gain(fixed_gain_factor,
01161 ff_dot_productf(ctx->fixed_vector, ctx->fixed_vector,
01162 AMRWB_SFR_SIZE) / AMRWB_SFR_SIZE,
01163 ctx->prediction_error,
01164 ENERGY_MEAN, energy_pred_fac);
01165
01166
01167 voice_fac = voice_factor(ctx->pitch_vector, ctx->pitch_gain[0],
01168 ctx->fixed_vector, ctx->fixed_gain[0]);
01169 ctx->tilt_coef = voice_fac * 0.25 + 0.25;
01170
01171
01172 for (i = 0; i < AMRWB_SFR_SIZE; i++) {
01173 ctx->excitation[i] *= ctx->pitch_gain[0];
01174 ctx->excitation[i] += ctx->fixed_gain[0] * ctx->fixed_vector[i];
01175 ctx->excitation[i] = truncf(ctx->excitation[i]);
01176 }
01177
01178
01179 synth_fixed_gain = noise_enhancer(ctx->fixed_gain[0], &ctx->prev_tr_gain,
01180 voice_fac, stab_fac);
01181
01182 synth_fixed_vector = anti_sparseness(ctx, ctx->fixed_vector,
01183 spare_vector);
01184
01185 pitch_enhancer(synth_fixed_vector, voice_fac);
01186
01187 synthesis(ctx, ctx->lp_coef[sub], synth_exc, synth_fixed_gain,
01188 synth_fixed_vector, &ctx->samples_az[LP_ORDER]);
01189
01190
01191 de_emphasis(&ctx->samples_up[UPS_MEM_SIZE],
01192 &ctx->samples_az[LP_ORDER], PREEMPH_FAC, ctx->demph_mem);
01193
01194 ff_acelp_apply_order_2_transfer_function(&ctx->samples_up[UPS_MEM_SIZE],
01195 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_31_poles,
01196 hpf_31_gain, ctx->hpf_31_mem, AMRWB_SFR_SIZE);
01197
01198 upsample_5_4(sub_buf, &ctx->samples_up[UPS_FIR_SIZE],
01199 AMRWB_SFR_SIZE_16k);
01200
01201
01202 ff_acelp_apply_order_2_transfer_function(hb_samples,
01203 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_400_poles,
01204 hpf_400_gain, ctx->hpf_400_mem, AMRWB_SFR_SIZE);
01205
01206 hb_gain = find_hb_gain(ctx, hb_samples,
01207 cur_subframe->hb_gain, cf->vad);
01208
01209 scaled_hb_excitation(ctx, hb_exc, synth_exc, hb_gain);
01210
01211 hb_synthesis(ctx, sub, &ctx->samples_hb[LP_ORDER_16k],
01212 hb_exc, ctx->isf_cur, ctx->isf_past_final);
01213
01214
01215 hb_fir_filter(hb_samples, bpf_6_7_coef, ctx->bpf_6_7_mem,
01216 &ctx->samples_hb[LP_ORDER_16k]);
01217
01218 if (ctx->fr_cur_mode == MODE_23k85)
01219 hb_fir_filter(hb_samples, lpf_7_coef, ctx->lpf_7_mem,
01220 hb_samples);
01221
01222
01223 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
01224 sub_buf[i] = (sub_buf[i] + hb_samples[i]) * (1.0f / (1 << 15));
01225
01226
01227 update_sub_state(ctx);
01228 }
01229
01230
01231 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(ctx->isp[3][0]));
01232 memcpy(ctx->isf_past_final, ctx->isf_cur, LP_ORDER * sizeof(float));
01233
01234 *got_frame_ptr = 1;
01235 *(AVFrame *)data = ctx->avframe;
01236
01237 return expected_fr_size;
01238 }
01239
01240 AVCodec ff_amrwb_decoder = {
01241 .name = "amrwb",
01242 .type = AVMEDIA_TYPE_AUDIO,
01243 .id = CODEC_ID_AMR_WB,
01244 .priv_data_size = sizeof(AMRWBContext),
01245 .init = amrwb_decode_init,
01246 .decode = amrwb_decode_frame,
01247 .capabilities = CODEC_CAP_DR1,
01248 .long_name = NULL_IF_CONFIG_SMALL("Adaptive Multi-Rate WideBand"),
01249 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
01250 };