00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/channel_layout.h"
00028 #include "libavutil/common.h"
00029 #include "libavutil/lfg.h"
00030
00031 #include "avcodec.h"
00032 #include "dsputil.h"
00033 #include "lsp.h"
00034 #include "celp_filters.h"
00035 #include "celp_math.h"
00036 #include "acelp_filters.h"
00037 #include "acelp_vectors.h"
00038 #include "acelp_pitch_delay.h"
00039 #include "internal.h"
00040
00041 #define AMR_USE_16BIT_TABLES
00042 #include "amr.h"
00043
00044 #include "amrwbdata.h"
00045 #include "mips/amrwbdec_mips.h"
00046
00047 typedef struct {
00048 AVFrame avframe;
00049 AMRWBFrame frame;
00050 enum Mode fr_cur_mode;
00051 uint8_t fr_quality;
00052 float isf_cur[LP_ORDER];
00053 float isf_q_past[LP_ORDER];
00054 float isf_past_final[LP_ORDER];
00055 double isp[4][LP_ORDER];
00056 double isp_sub4_past[LP_ORDER];
00057
00058 float lp_coef[4][LP_ORDER];
00059
00060 uint8_t base_pitch_lag;
00061 uint8_t pitch_lag_int;
00062
00063 float excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 2 + AMRWB_SFR_SIZE];
00064 float *excitation;
00065
00066 float pitch_vector[AMRWB_SFR_SIZE];
00067 float fixed_vector[AMRWB_SFR_SIZE];
00068
00069 float prediction_error[4];
00070 float pitch_gain[6];
00071 float fixed_gain[2];
00072
00073 float tilt_coef;
00074
00075 float prev_sparse_fixed_gain;
00076 uint8_t prev_ir_filter_nr;
00077 float prev_tr_gain;
00078
00079 float samples_az[LP_ORDER + AMRWB_SFR_SIZE];
00080 float samples_up[UPS_MEM_SIZE + AMRWB_SFR_SIZE];
00081 float samples_hb[LP_ORDER_16k + AMRWB_SFR_SIZE_16k];
00082
00083 float hpf_31_mem[2], hpf_400_mem[2];
00084 float demph_mem[1];
00085 float bpf_6_7_mem[HB_FIR_SIZE];
00086 float lpf_7_mem[HB_FIR_SIZE];
00087
00088 AVLFG prng;
00089 uint8_t first_frame;
00090 ACELPFContext acelpf_ctx;
00091 ACELPVContext acelpv_ctx;
00092 CELPFContext celpf_ctx;
00093 CELPMContext celpm_ctx;
00094
00095 } AMRWBContext;
00096
00097 static av_cold int amrwb_decode_init(AVCodecContext *avctx)
00098 {
00099 AMRWBContext *ctx = avctx->priv_data;
00100 int i;
00101
00102 if (avctx->channels > 1) {
00103 av_log_missing_feature(avctx, "multi-channel AMR", 0);
00104 return AVERROR_PATCHWELCOME;
00105 }
00106
00107 avctx->channels = 1;
00108 avctx->channel_layout = AV_CH_LAYOUT_MONO;
00109 if (!avctx->sample_rate)
00110 avctx->sample_rate = 16000;
00111 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00112
00113 av_lfg_init(&ctx->prng, 1);
00114
00115 ctx->excitation = &ctx->excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 1];
00116 ctx->first_frame = 1;
00117
00118 for (i = 0; i < LP_ORDER; i++)
00119 ctx->isf_past_final[i] = isf_init[i] * (1.0f / (1 << 15));
00120
00121 for (i = 0; i < 4; i++)
00122 ctx->prediction_error[i] = MIN_ENERGY;
00123
00124 avcodec_get_frame_defaults(&ctx->avframe);
00125 avctx->coded_frame = &ctx->avframe;
00126
00127 ff_acelp_filter_init(&ctx->acelpf_ctx);
00128 ff_acelp_vectors_init(&ctx->acelpv_ctx);
00129 ff_celp_filter_init(&ctx->celpf_ctx);
00130 ff_celp_math_init(&ctx->celpm_ctx);
00131
00132 return 0;
00133 }
00134
00144 static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf)
00145 {
00146
00147 ctx->fr_cur_mode = buf[0] >> 3 & 0x0F;
00148 ctx->fr_quality = (buf[0] & 0x4) == 0x4;
00149
00150 return 1;
00151 }
00152
00160 static void decode_isf_indices_36b(uint16_t *ind, float *isf_q)
00161 {
00162 int i;
00163
00164 for (i = 0; i < 9; i++)
00165 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
00166
00167 for (i = 0; i < 7; i++)
00168 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
00169
00170 for (i = 0; i < 5; i++)
00171 isf_q[i] += dico21_isf_36b[ind[2]][i] * (1.0f / (1 << 15));
00172
00173 for (i = 0; i < 4; i++)
00174 isf_q[i + 5] += dico22_isf_36b[ind[3]][i] * (1.0f / (1 << 15));
00175
00176 for (i = 0; i < 7; i++)
00177 isf_q[i + 9] += dico23_isf_36b[ind[4]][i] * (1.0f / (1 << 15));
00178 }
00179
00187 static void decode_isf_indices_46b(uint16_t *ind, float *isf_q)
00188 {
00189 int i;
00190
00191 for (i = 0; i < 9; i++)
00192 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
00193
00194 for (i = 0; i < 7; i++)
00195 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
00196
00197 for (i = 0; i < 3; i++)
00198 isf_q[i] += dico21_isf[ind[2]][i] * (1.0f / (1 << 15));
00199
00200 for (i = 0; i < 3; i++)
00201 isf_q[i + 3] += dico22_isf[ind[3]][i] * (1.0f / (1 << 15));
00202
00203 for (i = 0; i < 3; i++)
00204 isf_q[i + 6] += dico23_isf[ind[4]][i] * (1.0f / (1 << 15));
00205
00206 for (i = 0; i < 3; i++)
00207 isf_q[i + 9] += dico24_isf[ind[5]][i] * (1.0f / (1 << 15));
00208
00209 for (i = 0; i < 4; i++)
00210 isf_q[i + 12] += dico25_isf[ind[6]][i] * (1.0f / (1 << 15));
00211 }
00212
00221 static void isf_add_mean_and_past(float *isf_q, float *isf_past)
00222 {
00223 int i;
00224 float tmp;
00225
00226 for (i = 0; i < LP_ORDER; i++) {
00227 tmp = isf_q[i];
00228 isf_q[i] += isf_mean[i] * (1.0f / (1 << 15));
00229 isf_q[i] += PRED_FACTOR * isf_past[i];
00230 isf_past[i] = tmp;
00231 }
00232 }
00233
00241 static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past)
00242 {
00243 int i, k;
00244
00245 for (k = 0; k < 3; k++) {
00246 float c = isfp_inter[k];
00247 for (i = 0; i < LP_ORDER; i++)
00248 isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i];
00249 }
00250 }
00251
00263 static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index,
00264 uint8_t *base_lag_int, int subframe)
00265 {
00266 if (subframe == 0 || subframe == 2) {
00267 if (pitch_index < 376) {
00268 *lag_int = (pitch_index + 137) >> 2;
00269 *lag_frac = pitch_index - (*lag_int << 2) + 136;
00270 } else if (pitch_index < 440) {
00271 *lag_int = (pitch_index + 257 - 376) >> 1;
00272 *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) << 1;
00273
00274 } else {
00275 *lag_int = pitch_index - 280;
00276 *lag_frac = 0;
00277 }
00278
00279 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
00280 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15);
00281
00282
00283
00284 } else {
00285 *lag_int = (pitch_index + 1) >> 2;
00286 *lag_frac = pitch_index - (*lag_int << 2);
00287 *lag_int += *base_lag_int;
00288 }
00289 }
00290
00296 static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index,
00297 uint8_t *base_lag_int, int subframe, enum Mode mode)
00298 {
00299 if (subframe == 0 || (subframe == 2 && mode != MODE_6k60)) {
00300 if (pitch_index < 116) {
00301 *lag_int = (pitch_index + 69) >> 1;
00302 *lag_frac = (pitch_index - (*lag_int << 1) + 68) << 1;
00303 } else {
00304 *lag_int = pitch_index - 24;
00305 *lag_frac = 0;
00306 }
00307
00308 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
00309 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15);
00310 } else {
00311 *lag_int = (pitch_index + 1) >> 1;
00312 *lag_frac = (pitch_index - (*lag_int << 1)) << 1;
00313 *lag_int += *base_lag_int;
00314 }
00315 }
00316
00325 static void decode_pitch_vector(AMRWBContext *ctx,
00326 const AMRWBSubFrame *amr_subframe,
00327 const int subframe)
00328 {
00329 int pitch_lag_int, pitch_lag_frac;
00330 int i;
00331 float *exc = ctx->excitation;
00332 enum Mode mode = ctx->fr_cur_mode;
00333
00334 if (mode <= MODE_8k85) {
00335 decode_pitch_lag_low(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
00336 &ctx->base_pitch_lag, subframe, mode);
00337 } else
00338 decode_pitch_lag_high(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
00339 &ctx->base_pitch_lag, subframe);
00340
00341 ctx->pitch_lag_int = pitch_lag_int;
00342 pitch_lag_int += pitch_lag_frac > 0;
00343
00344
00345
00346 ctx->acelpf_ctx.acelp_interpolatef(exc,
00347 exc + 1 - pitch_lag_int,
00348 ac_inter, 4,
00349 pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4),
00350 LP_ORDER, AMRWB_SFR_SIZE + 1);
00351
00352
00353
00354 if (amr_subframe->ltp) {
00355 memcpy(ctx->pitch_vector, exc, AMRWB_SFR_SIZE * sizeof(float));
00356 } else {
00357 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00358 ctx->pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] +
00359 0.18 * exc[i + 1];
00360 memcpy(exc, ctx->pitch_vector, AMRWB_SFR_SIZE * sizeof(float));
00361 }
00362 }
00363
00365 #define BIT_STR(x,lsb,len) (((x) >> (lsb)) & ((1 << (len)) - 1))
00366
00368 #define BIT_POS(x, p) (((x) >> (p)) & 1)
00369
00383 static inline void decode_1p_track(int *out, int code, int m, int off)
00384 {
00385 int pos = BIT_STR(code, 0, m) + off;
00386
00387 out[0] = BIT_POS(code, m) ? -pos : pos;
00388 }
00389
00390 static inline void decode_2p_track(int *out, int code, int m, int off)
00391 {
00392 int pos0 = BIT_STR(code, m, m) + off;
00393 int pos1 = BIT_STR(code, 0, m) + off;
00394
00395 out[0] = BIT_POS(code, 2*m) ? -pos0 : pos0;
00396 out[1] = BIT_POS(code, 2*m) ? -pos1 : pos1;
00397 out[1] = pos0 > pos1 ? -out[1] : out[1];
00398 }
00399
00400 static void decode_3p_track(int *out, int code, int m, int off)
00401 {
00402 int half_2p = BIT_POS(code, 2*m - 1) << (m - 1);
00403
00404 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
00405 m - 1, off + half_2p);
00406 decode_1p_track(out + 2, BIT_STR(code, 2*m, m + 1), m, off);
00407 }
00408
00409 static void decode_4p_track(int *out, int code, int m, int off)
00410 {
00411 int half_4p, subhalf_2p;
00412 int b_offset = 1 << (m - 1);
00413
00414 switch (BIT_STR(code, 4*m - 2, 2)) {
00415 case 0:
00416 half_4p = BIT_POS(code, 4*m - 3) << (m - 1);
00417 subhalf_2p = BIT_POS(code, 2*m - 3) << (m - 2);
00418
00419 decode_2p_track(out, BIT_STR(code, 0, 2*m - 3),
00420 m - 2, off + half_4p + subhalf_2p);
00421 decode_2p_track(out + 2, BIT_STR(code, 2*m - 2, 2*m - 1),
00422 m - 1, off + half_4p);
00423 break;
00424 case 1:
00425 decode_1p_track(out, BIT_STR(code, 3*m - 2, m),
00426 m - 1, off);
00427 decode_3p_track(out + 1, BIT_STR(code, 0, 3*m - 2),
00428 m - 1, off + b_offset);
00429 break;
00430 case 2:
00431 decode_2p_track(out, BIT_STR(code, 2*m - 1, 2*m - 1),
00432 m - 1, off);
00433 decode_2p_track(out + 2, BIT_STR(code, 0, 2*m - 1),
00434 m - 1, off + b_offset);
00435 break;
00436 case 3:
00437 decode_3p_track(out, BIT_STR(code, m, 3*m - 2),
00438 m - 1, off);
00439 decode_1p_track(out + 3, BIT_STR(code, 0, m),
00440 m - 1, off + b_offset);
00441 break;
00442 }
00443 }
00444
00445 static void decode_5p_track(int *out, int code, int m, int off)
00446 {
00447 int half_3p = BIT_POS(code, 5*m - 1) << (m - 1);
00448
00449 decode_3p_track(out, BIT_STR(code, 2*m + 1, 3*m - 2),
00450 m - 1, off + half_3p);
00451
00452 decode_2p_track(out + 3, BIT_STR(code, 0, 2*m + 1), m, off);
00453 }
00454
00455 static void decode_6p_track(int *out, int code, int m, int off)
00456 {
00457 int b_offset = 1 << (m - 1);
00458
00459 int half_more = BIT_POS(code, 6*m - 5) << (m - 1);
00460 int half_other = b_offset - half_more;
00461
00462 switch (BIT_STR(code, 6*m - 4, 2)) {
00463 case 0:
00464 decode_1p_track(out, BIT_STR(code, 0, m),
00465 m - 1, off + half_more);
00466 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
00467 m - 1, off + half_more);
00468 break;
00469 case 1:
00470 decode_1p_track(out, BIT_STR(code, 0, m),
00471 m - 1, off + half_other);
00472 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
00473 m - 1, off + half_more);
00474 break;
00475 case 2:
00476 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
00477 m - 1, off + half_other);
00478 decode_4p_track(out + 2, BIT_STR(code, 2*m - 1, 4*m - 4),
00479 m - 1, off + half_more);
00480 break;
00481 case 3:
00482 decode_3p_track(out, BIT_STR(code, 3*m - 2, 3*m - 2),
00483 m - 1, off);
00484 decode_3p_track(out + 3, BIT_STR(code, 0, 3*m - 2),
00485 m - 1, off + b_offset);
00486 break;
00487 }
00488 }
00489
00499 static void decode_fixed_vector(float *fixed_vector, const uint16_t *pulse_hi,
00500 const uint16_t *pulse_lo, const enum Mode mode)
00501 {
00502
00503
00504 int sig_pos[4][6];
00505 int spacing = (mode == MODE_6k60) ? 2 : 4;
00506 int i, j;
00507
00508 switch (mode) {
00509 case MODE_6k60:
00510 for (i = 0; i < 2; i++)
00511 decode_1p_track(sig_pos[i], pulse_lo[i], 5, 1);
00512 break;
00513 case MODE_8k85:
00514 for (i = 0; i < 4; i++)
00515 decode_1p_track(sig_pos[i], pulse_lo[i], 4, 1);
00516 break;
00517 case MODE_12k65:
00518 for (i = 0; i < 4; i++)
00519 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
00520 break;
00521 case MODE_14k25:
00522 for (i = 0; i < 2; i++)
00523 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
00524 for (i = 2; i < 4; i++)
00525 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
00526 break;
00527 case MODE_15k85:
00528 for (i = 0; i < 4; i++)
00529 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
00530 break;
00531 case MODE_18k25:
00532 for (i = 0; i < 4; i++)
00533 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
00534 ((int) pulse_hi[i] << 14), 4, 1);
00535 break;
00536 case MODE_19k85:
00537 for (i = 0; i < 2; i++)
00538 decode_5p_track(sig_pos[i], (int) pulse_lo[i] +
00539 ((int) pulse_hi[i] << 10), 4, 1);
00540 for (i = 2; i < 4; i++)
00541 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
00542 ((int) pulse_hi[i] << 14), 4, 1);
00543 break;
00544 case MODE_23k05:
00545 case MODE_23k85:
00546 for (i = 0; i < 4; i++)
00547 decode_6p_track(sig_pos[i], (int) pulse_lo[i] +
00548 ((int) pulse_hi[i] << 11), 4, 1);
00549 break;
00550 }
00551
00552 memset(fixed_vector, 0, sizeof(float) * AMRWB_SFR_SIZE);
00553
00554 for (i = 0; i < 4; i++)
00555 for (j = 0; j < pulses_nb_per_mode_tr[mode][i]; j++) {
00556 int pos = (FFABS(sig_pos[i][j]) - 1) * spacing + i;
00557
00558 fixed_vector[pos] += sig_pos[i][j] < 0 ? -1.0 : 1.0;
00559 }
00560 }
00561
00570 static void decode_gains(const uint8_t vq_gain, const enum Mode mode,
00571 float *fixed_gain_factor, float *pitch_gain)
00572 {
00573 const int16_t *gains = (mode <= MODE_8k85 ? qua_gain_6b[vq_gain] :
00574 qua_gain_7b[vq_gain]);
00575
00576 *pitch_gain = gains[0] * (1.0f / (1 << 14));
00577 *fixed_gain_factor = gains[1] * (1.0f / (1 << 11));
00578 }
00579
00586
00587
00588 static void pitch_sharpening(AMRWBContext *ctx, float *fixed_vector)
00589 {
00590 int i;
00591
00592
00593 for (i = AMRWB_SFR_SIZE - 1; i != 0; i--)
00594 fixed_vector[i] -= fixed_vector[i - 1] * ctx->tilt_coef;
00595
00596
00597 for (i = ctx->pitch_lag_int; i < AMRWB_SFR_SIZE; i++)
00598 fixed_vector[i] += fixed_vector[i - ctx->pitch_lag_int] * 0.85;
00599 }
00600
00608
00609
00610 static float voice_factor(float *p_vector, float p_gain,
00611 float *f_vector, float f_gain,
00612 CELPMContext *ctx)
00613 {
00614 double p_ener = (double) ctx->dot_productf(p_vector, p_vector,
00615 AMRWB_SFR_SIZE) *
00616 p_gain * p_gain;
00617 double f_ener = (double) ctx->dot_productf(f_vector, f_vector,
00618 AMRWB_SFR_SIZE) *
00619 f_gain * f_gain;
00620
00621 return (p_ener - f_ener) / (p_ener + f_ener);
00622 }
00623
00634 static float *anti_sparseness(AMRWBContext *ctx,
00635 float *fixed_vector, float *buf)
00636 {
00637 int ir_filter_nr;
00638
00639 if (ctx->fr_cur_mode > MODE_8k85)
00640 return fixed_vector;
00641
00642 if (ctx->pitch_gain[0] < 0.6) {
00643 ir_filter_nr = 0;
00644 } else if (ctx->pitch_gain[0] < 0.9) {
00645 ir_filter_nr = 1;
00646 } else
00647 ir_filter_nr = 2;
00648
00649
00650 if (ctx->fixed_gain[0] > 3.0 * ctx->fixed_gain[1]) {
00651 if (ir_filter_nr < 2)
00652 ir_filter_nr++;
00653 } else {
00654 int i, count = 0;
00655
00656 for (i = 0; i < 6; i++)
00657 if (ctx->pitch_gain[i] < 0.6)
00658 count++;
00659
00660 if (count > 2)
00661 ir_filter_nr = 0;
00662
00663 if (ir_filter_nr > ctx->prev_ir_filter_nr + 1)
00664 ir_filter_nr--;
00665 }
00666
00667
00668 ctx->prev_ir_filter_nr = ir_filter_nr;
00669
00670 ir_filter_nr += (ctx->fr_cur_mode == MODE_8k85);
00671
00672 if (ir_filter_nr < 2) {
00673 int i;
00674 const float *coef = ir_filters_lookup[ir_filter_nr];
00675
00676
00677
00678
00679
00680
00681
00682
00683 memset(buf, 0, sizeof(float) * AMRWB_SFR_SIZE);
00684 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00685 if (fixed_vector[i])
00686 ff_celp_circ_addf(buf, buf, coef, i, fixed_vector[i],
00687 AMRWB_SFR_SIZE);
00688 fixed_vector = buf;
00689 }
00690
00691 return fixed_vector;
00692 }
00693
00698 static float stability_factor(const float *isf, const float *isf_past)
00699 {
00700 int i;
00701 float acc = 0.0;
00702
00703 for (i = 0; i < LP_ORDER - 1; i++)
00704 acc += (isf[i] - isf_past[i]) * (isf[i] - isf_past[i]);
00705
00706
00707
00708 return FFMAX(0.0, 1.25 - acc * 0.8 * 512);
00709 }
00710
00722 static float noise_enhancer(float fixed_gain, float *prev_tr_gain,
00723 float voice_fac, float stab_fac)
00724 {
00725 float sm_fac = 0.5 * (1 - voice_fac) * stab_fac;
00726 float g0;
00727
00728
00729
00730
00731 if (fixed_gain < *prev_tr_gain) {
00732 g0 = FFMIN(*prev_tr_gain, fixed_gain + fixed_gain *
00733 (6226 * (1.0f / (1 << 15))));
00734 } else
00735 g0 = FFMAX(*prev_tr_gain, fixed_gain *
00736 (27536 * (1.0f / (1 << 15))));
00737
00738 *prev_tr_gain = g0;
00739
00740 return sm_fac * g0 + (1 - sm_fac) * fixed_gain;
00741 }
00742
00749 static void pitch_enhancer(float *fixed_vector, float voice_fac)
00750 {
00751 int i;
00752 float cpe = 0.125 * (1 + voice_fac);
00753 float last = fixed_vector[0];
00754
00755 fixed_vector[0] -= cpe * fixed_vector[1];
00756
00757 for (i = 1; i < AMRWB_SFR_SIZE - 1; i++) {
00758 float cur = fixed_vector[i];
00759
00760 fixed_vector[i] -= cpe * (last + fixed_vector[i + 1]);
00761 last = cur;
00762 }
00763
00764 fixed_vector[AMRWB_SFR_SIZE - 1] -= cpe * last;
00765 }
00766
00777 static void synthesis(AMRWBContext *ctx, float *lpc, float *excitation,
00778 float fixed_gain, const float *fixed_vector,
00779 float *samples)
00780 {
00781 ctx->acelpv_ctx.weighted_vector_sumf(excitation, ctx->pitch_vector, fixed_vector,
00782 ctx->pitch_gain[0], fixed_gain, AMRWB_SFR_SIZE);
00783
00784
00785 if (ctx->pitch_gain[0] > 0.5 && ctx->fr_cur_mode <= MODE_8k85) {
00786 int i;
00787 float energy = ctx->celpm_ctx.dot_productf(excitation, excitation,
00788 AMRWB_SFR_SIZE);
00789
00790
00791
00792 float pitch_factor = 0.25 * ctx->pitch_gain[0] * ctx->pitch_gain[0];
00793
00794 for (i = 0; i < AMRWB_SFR_SIZE; i++)
00795 excitation[i] += pitch_factor * ctx->pitch_vector[i];
00796
00797 ff_scale_vector_to_given_sum_of_squares(excitation, excitation,
00798 energy, AMRWB_SFR_SIZE);
00799 }
00800
00801 ctx->celpf_ctx.celp_lp_synthesis_filterf(samples, lpc, excitation,
00802 AMRWB_SFR_SIZE, LP_ORDER);
00803 }
00804
00814 static void de_emphasis(float *out, float *in, float m, float mem[1])
00815 {
00816 int i;
00817
00818 out[0] = in[0] + m * mem[0];
00819
00820 for (i = 1; i < AMRWB_SFR_SIZE; i++)
00821 out[i] = in[i] + out[i - 1] * m;
00822
00823 mem[0] = out[AMRWB_SFR_SIZE - 1];
00824 }
00825
00835 static void upsample_5_4(float *out, const float *in, int o_size, CELPMContext *ctx)
00836 {
00837 const float *in0 = in - UPS_FIR_SIZE + 1;
00838 int i, j, k;
00839 int int_part = 0, frac_part;
00840
00841 i = 0;
00842 for (j = 0; j < o_size / 5; j++) {
00843 out[i] = in[int_part];
00844 frac_part = 4;
00845 i++;
00846
00847 for (k = 1; k < 5; k++) {
00848 out[i] = ctx->dot_productf(in0 + int_part,
00849 upsample_fir[4 - frac_part],
00850 UPS_MEM_SIZE);
00851 int_part++;
00852 frac_part--;
00853 i++;
00854 }
00855 }
00856 }
00857
00867 static float find_hb_gain(AMRWBContext *ctx, const float *synth,
00868 uint16_t hb_idx, uint8_t vad)
00869 {
00870 int wsp = (vad > 0);
00871 float tilt;
00872
00873 if (ctx->fr_cur_mode == MODE_23k85)
00874 return qua_hb_gain[hb_idx] * (1.0f / (1 << 14));
00875
00876 tilt = ctx->celpm_ctx.dot_productf(synth, synth + 1, AMRWB_SFR_SIZE - 1) /
00877 ctx->celpm_ctx.dot_productf(synth, synth, AMRWB_SFR_SIZE);
00878
00879
00880 return av_clipf((1.0 - FFMAX(0.0, tilt)) * (1.25 - 0.25 * wsp), 0.1, 1.0);
00881 }
00882
00892 static void scaled_hb_excitation(AMRWBContext *ctx, float *hb_exc,
00893 const float *synth_exc, float hb_gain)
00894 {
00895 int i;
00896 float energy = ctx->celpm_ctx.dot_productf(synth_exc, synth_exc, AMRWB_SFR_SIZE);
00897
00898
00899 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
00900 hb_exc[i] = 32768.0 - (uint16_t) av_lfg_get(&ctx->prng);
00901
00902 ff_scale_vector_to_given_sum_of_squares(hb_exc, hb_exc,
00903 energy * hb_gain * hb_gain,
00904 AMRWB_SFR_SIZE_16k);
00905 }
00906
00910 static float auto_correlation(float *diff_isf, float mean, int lag)
00911 {
00912 int i;
00913 float sum = 0.0;
00914
00915 for (i = 7; i < LP_ORDER - 2; i++) {
00916 float prod = (diff_isf[i] - mean) * (diff_isf[i - lag] - mean);
00917 sum += prod * prod;
00918 }
00919 return sum;
00920 }
00921
00929 static void extrapolate_isf(float isf[LP_ORDER_16k])
00930 {
00931 float diff_isf[LP_ORDER - 2], diff_mean;
00932 float corr_lag[3];
00933 float est, scale;
00934 int i, j, i_max_corr;
00935
00936 isf[LP_ORDER_16k - 1] = isf[LP_ORDER - 1];
00937
00938
00939 for (i = 0; i < LP_ORDER - 2; i++)
00940 diff_isf[i] = isf[i + 1] - isf[i];
00941
00942 diff_mean = 0.0;
00943 for (i = 2; i < LP_ORDER - 2; i++)
00944 diff_mean += diff_isf[i] * (1.0f / (LP_ORDER - 4));
00945
00946
00947 i_max_corr = 0;
00948 for (i = 0; i < 3; i++) {
00949 corr_lag[i] = auto_correlation(diff_isf, diff_mean, i + 2);
00950
00951 if (corr_lag[i] > corr_lag[i_max_corr])
00952 i_max_corr = i;
00953 }
00954 i_max_corr++;
00955
00956 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
00957 isf[i] = isf[i - 1] + isf[i - 1 - i_max_corr]
00958 - isf[i - 2 - i_max_corr];
00959
00960
00961 est = 7965 + (isf[2] - isf[3] - isf[4]) / 6.0;
00962 scale = 0.5 * (FFMIN(est, 7600) - isf[LP_ORDER - 2]) /
00963 (isf[LP_ORDER_16k - 2] - isf[LP_ORDER - 2]);
00964
00965 for (i = LP_ORDER - 1, j = 0; i < LP_ORDER_16k - 1; i++, j++)
00966 diff_isf[j] = scale * (isf[i] - isf[i - 1]);
00967
00968
00969 for (i = 1; i < LP_ORDER_16k - LP_ORDER; i++)
00970 if (diff_isf[i] + diff_isf[i - 1] < 5.0) {
00971 if (diff_isf[i] > diff_isf[i - 1]) {
00972 diff_isf[i - 1] = 5.0 - diff_isf[i];
00973 } else
00974 diff_isf[i] = 5.0 - diff_isf[i - 1];
00975 }
00976
00977 for (i = LP_ORDER - 1, j = 0; i < LP_ORDER_16k - 1; i++, j++)
00978 isf[i] = isf[i - 1] + diff_isf[j] * (1.0f / (1 << 15));
00979
00980
00981 for (i = 0; i < LP_ORDER_16k - 1; i++)
00982 isf[i] *= 0.8;
00983 }
00984
00994 static void lpc_weighting(float *out, const float *lpc, float gamma, int size)
00995 {
00996 int i;
00997 float fac = gamma;
00998
00999 for (i = 0; i < size; i++) {
01000 out[i] = lpc[i] * fac;
01001 fac *= gamma;
01002 }
01003 }
01004
01016 static void hb_synthesis(AMRWBContext *ctx, int subframe, float *samples,
01017 const float *exc, const float *isf, const float *isf_past)
01018 {
01019 float hb_lpc[LP_ORDER_16k];
01020 enum Mode mode = ctx->fr_cur_mode;
01021
01022 if (mode == MODE_6k60) {
01023 float e_isf[LP_ORDER_16k];
01024 double e_isp[LP_ORDER_16k];
01025
01026 ctx->acelpv_ctx.weighted_vector_sumf(e_isf, isf_past, isf, isfp_inter[subframe],
01027 1.0 - isfp_inter[subframe], LP_ORDER);
01028
01029 extrapolate_isf(e_isf);
01030
01031 e_isf[LP_ORDER_16k - 1] *= 2.0;
01032 ff_acelp_lsf2lspd(e_isp, e_isf, LP_ORDER_16k);
01033 ff_amrwb_lsp2lpc(e_isp, hb_lpc, LP_ORDER_16k);
01034
01035 lpc_weighting(hb_lpc, hb_lpc, 0.9, LP_ORDER_16k);
01036 } else {
01037 lpc_weighting(hb_lpc, ctx->lp_coef[subframe], 0.6, LP_ORDER);
01038 }
01039
01040 ctx->celpf_ctx.celp_lp_synthesis_filterf(samples, hb_lpc, exc, AMRWB_SFR_SIZE_16k,
01041 (mode == MODE_6k60) ? LP_ORDER_16k : LP_ORDER);
01042 }
01043
01056 #ifndef hb_fir_filter
01057 static void hb_fir_filter(float *out, const float fir_coef[HB_FIR_SIZE + 1],
01058 float mem[HB_FIR_SIZE], const float *in)
01059 {
01060 int i, j;
01061 float data[AMRWB_SFR_SIZE_16k + HB_FIR_SIZE];
01062
01063 memcpy(data, mem, HB_FIR_SIZE * sizeof(float));
01064 memcpy(data + HB_FIR_SIZE, in, AMRWB_SFR_SIZE_16k * sizeof(float));
01065
01066 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++) {
01067 out[i] = 0.0;
01068 for (j = 0; j <= HB_FIR_SIZE; j++)
01069 out[i] += data[i + j] * fir_coef[j];
01070 }
01071
01072 memcpy(mem, data + AMRWB_SFR_SIZE_16k, HB_FIR_SIZE * sizeof(float));
01073 }
01074 #endif
01075
01079 static void update_sub_state(AMRWBContext *ctx)
01080 {
01081 memmove(&ctx->excitation_buf[0], &ctx->excitation_buf[AMRWB_SFR_SIZE],
01082 (AMRWB_P_DELAY_MAX + LP_ORDER + 1) * sizeof(float));
01083
01084 memmove(&ctx->pitch_gain[1], &ctx->pitch_gain[0], 5 * sizeof(float));
01085 memmove(&ctx->fixed_gain[1], &ctx->fixed_gain[0], 1 * sizeof(float));
01086
01087 memmove(&ctx->samples_az[0], &ctx->samples_az[AMRWB_SFR_SIZE],
01088 LP_ORDER * sizeof(float));
01089 memmove(&ctx->samples_up[0], &ctx->samples_up[AMRWB_SFR_SIZE],
01090 UPS_MEM_SIZE * sizeof(float));
01091 memmove(&ctx->samples_hb[0], &ctx->samples_hb[AMRWB_SFR_SIZE_16k],
01092 LP_ORDER_16k * sizeof(float));
01093 }
01094
01095 static int amrwb_decode_frame(AVCodecContext *avctx, void *data,
01096 int *got_frame_ptr, AVPacket *avpkt)
01097 {
01098 AMRWBContext *ctx = avctx->priv_data;
01099 AMRWBFrame *cf = &ctx->frame;
01100 const uint8_t *buf = avpkt->data;
01101 int buf_size = avpkt->size;
01102 int expected_fr_size, header_size;
01103 float *buf_out;
01104 float spare_vector[AMRWB_SFR_SIZE];
01105 float fixed_gain_factor;
01106 float *synth_fixed_vector;
01107 float synth_fixed_gain;
01108 float voice_fac, stab_fac;
01109 float synth_exc[AMRWB_SFR_SIZE];
01110 float hb_exc[AMRWB_SFR_SIZE_16k];
01111 float hb_samples[AMRWB_SFR_SIZE_16k];
01112 float hb_gain;
01113 int sub, i, ret;
01114
01115
01116 ctx->avframe.nb_samples = 4 * AMRWB_SFR_SIZE_16k;
01117 if ((ret = ff_get_buffer(avctx, &ctx->avframe)) < 0) {
01118 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01119 return ret;
01120 }
01121 buf_out = (float *)ctx->avframe.data[0];
01122
01123 header_size = decode_mime_header(ctx, buf);
01124 if (ctx->fr_cur_mode > MODE_SID) {
01125 av_log(avctx, AV_LOG_ERROR,
01126 "Invalid mode %d\n", ctx->fr_cur_mode);
01127 return AVERROR_INVALIDDATA;
01128 }
01129 expected_fr_size = ((cf_sizes_wb[ctx->fr_cur_mode] + 7) >> 3) + 1;
01130
01131 if (buf_size < expected_fr_size) {
01132 av_log(avctx, AV_LOG_ERROR,
01133 "Frame too small (%d bytes). Truncated file?\n", buf_size);
01134 *got_frame_ptr = 0;
01135 return AVERROR_INVALIDDATA;
01136 }
01137
01138 if (!ctx->fr_quality || ctx->fr_cur_mode > MODE_SID)
01139 av_log(avctx, AV_LOG_ERROR, "Encountered a bad or corrupted frame\n");
01140
01141 if (ctx->fr_cur_mode == MODE_SID) {
01142 av_log_missing_feature(avctx, "SID mode", 1);
01143 return AVERROR_PATCHWELCOME;
01144 }
01145
01146 ff_amr_bit_reorder((uint16_t *) &ctx->frame, sizeof(AMRWBFrame),
01147 buf + header_size, amr_bit_orderings_by_mode[ctx->fr_cur_mode]);
01148
01149
01150 if (ctx->fr_cur_mode == MODE_6k60) {
01151 decode_isf_indices_36b(cf->isp_id, ctx->isf_cur);
01152 } else {
01153 decode_isf_indices_46b(cf->isp_id, ctx->isf_cur);
01154 }
01155
01156 isf_add_mean_and_past(ctx->isf_cur, ctx->isf_q_past);
01157 ff_set_min_dist_lsf(ctx->isf_cur, MIN_ISF_SPACING, LP_ORDER - 1);
01158
01159 stab_fac = stability_factor(ctx->isf_cur, ctx->isf_past_final);
01160
01161 ctx->isf_cur[LP_ORDER - 1] *= 2.0;
01162 ff_acelp_lsf2lspd(ctx->isp[3], ctx->isf_cur, LP_ORDER);
01163
01164
01165 if (ctx->first_frame) {
01166 ctx->first_frame = 0;
01167 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(double));
01168 }
01169 interpolate_isp(ctx->isp, ctx->isp_sub4_past);
01170
01171 for (sub = 0; sub < 4; sub++)
01172 ff_amrwb_lsp2lpc(ctx->isp[sub], ctx->lp_coef[sub], LP_ORDER);
01173
01174 for (sub = 0; sub < 4; sub++) {
01175 const AMRWBSubFrame *cur_subframe = &cf->subframe[sub];
01176 float *sub_buf = buf_out + sub * AMRWB_SFR_SIZE_16k;
01177
01178
01179 decode_pitch_vector(ctx, cur_subframe, sub);
01180
01181 decode_fixed_vector(ctx->fixed_vector, cur_subframe->pul_ih,
01182 cur_subframe->pul_il, ctx->fr_cur_mode);
01183
01184 pitch_sharpening(ctx, ctx->fixed_vector);
01185
01186 decode_gains(cur_subframe->vq_gain, ctx->fr_cur_mode,
01187 &fixed_gain_factor, &ctx->pitch_gain[0]);
01188
01189 ctx->fixed_gain[0] =
01190 ff_amr_set_fixed_gain(fixed_gain_factor,
01191 ctx->celpm_ctx.dot_productf(ctx->fixed_vector,
01192 ctx->fixed_vector,
01193 AMRWB_SFR_SIZE) /
01194 AMRWB_SFR_SIZE,
01195 ctx->prediction_error,
01196 ENERGY_MEAN, energy_pred_fac);
01197
01198
01199 voice_fac = voice_factor(ctx->pitch_vector, ctx->pitch_gain[0],
01200 ctx->fixed_vector, ctx->fixed_gain[0],
01201 &ctx->celpm_ctx);
01202 ctx->tilt_coef = voice_fac * 0.25 + 0.25;
01203
01204
01205 for (i = 0; i < AMRWB_SFR_SIZE; i++) {
01206 ctx->excitation[i] *= ctx->pitch_gain[0];
01207 ctx->excitation[i] += ctx->fixed_gain[0] * ctx->fixed_vector[i];
01208 ctx->excitation[i] = truncf(ctx->excitation[i]);
01209 }
01210
01211
01212 synth_fixed_gain = noise_enhancer(ctx->fixed_gain[0], &ctx->prev_tr_gain,
01213 voice_fac, stab_fac);
01214
01215 synth_fixed_vector = anti_sparseness(ctx, ctx->fixed_vector,
01216 spare_vector);
01217
01218 pitch_enhancer(synth_fixed_vector, voice_fac);
01219
01220 synthesis(ctx, ctx->lp_coef[sub], synth_exc, synth_fixed_gain,
01221 synth_fixed_vector, &ctx->samples_az[LP_ORDER]);
01222
01223
01224 de_emphasis(&ctx->samples_up[UPS_MEM_SIZE],
01225 &ctx->samples_az[LP_ORDER], PREEMPH_FAC, ctx->demph_mem);
01226
01227 ctx->acelpf_ctx.acelp_apply_order_2_transfer_function(&ctx->samples_up[UPS_MEM_SIZE],
01228 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_31_poles,
01229 hpf_31_gain, ctx->hpf_31_mem, AMRWB_SFR_SIZE);
01230
01231 upsample_5_4(sub_buf, &ctx->samples_up[UPS_FIR_SIZE],
01232 AMRWB_SFR_SIZE_16k, &ctx->celpm_ctx);
01233
01234
01235 ctx->acelpf_ctx.acelp_apply_order_2_transfer_function(hb_samples,
01236 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_400_poles,
01237 hpf_400_gain, ctx->hpf_400_mem, AMRWB_SFR_SIZE);
01238
01239 hb_gain = find_hb_gain(ctx, hb_samples,
01240 cur_subframe->hb_gain, cf->vad);
01241
01242 scaled_hb_excitation(ctx, hb_exc, synth_exc, hb_gain);
01243
01244 hb_synthesis(ctx, sub, &ctx->samples_hb[LP_ORDER_16k],
01245 hb_exc, ctx->isf_cur, ctx->isf_past_final);
01246
01247
01248 hb_fir_filter(hb_samples, bpf_6_7_coef, ctx->bpf_6_7_mem,
01249 &ctx->samples_hb[LP_ORDER_16k]);
01250
01251 if (ctx->fr_cur_mode == MODE_23k85)
01252 hb_fir_filter(hb_samples, lpf_7_coef, ctx->lpf_7_mem,
01253 hb_samples);
01254
01255
01256 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
01257 sub_buf[i] = (sub_buf[i] + hb_samples[i]) * (1.0f / (1 << 15));
01258
01259
01260 update_sub_state(ctx);
01261 }
01262
01263
01264 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(ctx->isp[3][0]));
01265 memcpy(ctx->isf_past_final, ctx->isf_cur, LP_ORDER * sizeof(float));
01266
01267 *got_frame_ptr = 1;
01268 *(AVFrame *)data = ctx->avframe;
01269
01270 return expected_fr_size;
01271 }
01272
01273 AVCodec ff_amrwb_decoder = {
01274 .name = "amrwb",
01275 .type = AVMEDIA_TYPE_AUDIO,
01276 .id = AV_CODEC_ID_AMR_WB,
01277 .priv_data_size = sizeof(AMRWBContext),
01278 .init = amrwb_decode_init,
01279 .decode = amrwb_decode_frame,
01280 .capabilities = CODEC_CAP_DR1,
01281 .long_name = NULL_IF_CONFIG_SMALL("AMR-WB (Adaptive Multi-Rate WideBand)"),
01282 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
01283 AV_SAMPLE_FMT_NONE },
01284 };