Libav 0.7.1
|
00001 /* 00002 * AMR wideband decoder 00003 * Copyright (c) 2010 Marcelo Galvao Povoa 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A particular PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 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 return 0; 00106 } 00107 00117 static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf) 00118 { 00119 GetBitContext gb; 00120 init_get_bits(&gb, buf, 8); 00121 00122 /* Decode frame header (1st octet) */ 00123 skip_bits(&gb, 1); // padding bit 00124 ctx->fr_cur_mode = get_bits(&gb, 4); 00125 ctx->fr_quality = get_bits1(&gb); 00126 skip_bits(&gb, 2); // padding bits 00127 00128 return 1; 00129 } 00130 00138 static void decode_isf_indices_36b(uint16_t *ind, float *isf_q) 00139 { 00140 int i; 00141 00142 for (i = 0; i < 9; i++) 00143 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15)); 00144 00145 for (i = 0; i < 7; i++) 00146 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15)); 00147 00148 for (i = 0; i < 5; i++) 00149 isf_q[i] += dico21_isf_36b[ind[2]][i] * (1.0f / (1 << 15)); 00150 00151 for (i = 0; i < 4; i++) 00152 isf_q[i + 5] += dico22_isf_36b[ind[3]][i] * (1.0f / (1 << 15)); 00153 00154 for (i = 0; i < 7; i++) 00155 isf_q[i + 9] += dico23_isf_36b[ind[4]][i] * (1.0f / (1 << 15)); 00156 } 00157 00165 static void decode_isf_indices_46b(uint16_t *ind, float *isf_q) 00166 { 00167 int i; 00168 00169 for (i = 0; i < 9; i++) 00170 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15)); 00171 00172 for (i = 0; i < 7; i++) 00173 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15)); 00174 00175 for (i = 0; i < 3; i++) 00176 isf_q[i] += dico21_isf[ind[2]][i] * (1.0f / (1 << 15)); 00177 00178 for (i = 0; i < 3; i++) 00179 isf_q[i + 3] += dico22_isf[ind[3]][i] * (1.0f / (1 << 15)); 00180 00181 for (i = 0; i < 3; i++) 00182 isf_q[i + 6] += dico23_isf[ind[4]][i] * (1.0f / (1 << 15)); 00183 00184 for (i = 0; i < 3; i++) 00185 isf_q[i + 9] += dico24_isf[ind[5]][i] * (1.0f / (1 << 15)); 00186 00187 for (i = 0; i < 4; i++) 00188 isf_q[i + 12] += dico25_isf[ind[6]][i] * (1.0f / (1 << 15)); 00189 } 00190 00199 static void isf_add_mean_and_past(float *isf_q, float *isf_past) 00200 { 00201 int i; 00202 float tmp; 00203 00204 for (i = 0; i < LP_ORDER; i++) { 00205 tmp = isf_q[i]; 00206 isf_q[i] += isf_mean[i] * (1.0f / (1 << 15)); 00207 isf_q[i] += PRED_FACTOR * isf_past[i]; 00208 isf_past[i] = tmp; 00209 } 00210 } 00211 00219 static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past) 00220 { 00221 int i, k; 00222 00223 for (k = 0; k < 3; k++) { 00224 float c = isfp_inter[k]; 00225 for (i = 0; i < LP_ORDER; i++) 00226 isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i]; 00227 } 00228 } 00229 00241 static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index, 00242 uint8_t *base_lag_int, int subframe) 00243 { 00244 if (subframe == 0 || subframe == 2) { 00245 if (pitch_index < 376) { 00246 *lag_int = (pitch_index + 137) >> 2; 00247 *lag_frac = pitch_index - (*lag_int << 2) + 136; 00248 } else if (pitch_index < 440) { 00249 *lag_int = (pitch_index + 257 - 376) >> 1; 00250 *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) << 1; 00251 /* the actual resolution is 1/2 but expressed as 1/4 */ 00252 } else { 00253 *lag_int = pitch_index - 280; 00254 *lag_frac = 0; 00255 } 00256 /* minimum lag for next subframe */ 00257 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0), 00258 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15); 00259 // XXX: the spec states clearly that *base_lag_int should be 00260 // the nearest integer to *lag_int (minus 8), but the ref code 00261 // actually always uses its floor, I'm following the latter 00262 } else { 00263 *lag_int = (pitch_index + 1) >> 2; 00264 *lag_frac = pitch_index - (*lag_int << 2); 00265 *lag_int += *base_lag_int; 00266 } 00267 } 00268 00274 static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index, 00275 uint8_t *base_lag_int, int subframe, enum Mode mode) 00276 { 00277 if (subframe == 0 || (subframe == 2 && mode != MODE_6k60)) { 00278 if (pitch_index < 116) { 00279 *lag_int = (pitch_index + 69) >> 1; 00280 *lag_frac = (pitch_index - (*lag_int << 1) + 68) << 1; 00281 } else { 00282 *lag_int = pitch_index - 24; 00283 *lag_frac = 0; 00284 } 00285 // XXX: same problem as before 00286 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0), 00287 AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15); 00288 } else { 00289 *lag_int = (pitch_index + 1) >> 1; 00290 *lag_frac = (pitch_index - (*lag_int << 1)) << 1; 00291 *lag_int += *base_lag_int; 00292 } 00293 } 00294 00303 static void decode_pitch_vector(AMRWBContext *ctx, 00304 const AMRWBSubFrame *amr_subframe, 00305 const int subframe) 00306 { 00307 int pitch_lag_int, pitch_lag_frac; 00308 int i; 00309 float *exc = ctx->excitation; 00310 enum Mode mode = ctx->fr_cur_mode; 00311 00312 if (mode <= MODE_8k85) { 00313 decode_pitch_lag_low(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap, 00314 &ctx->base_pitch_lag, subframe, mode); 00315 } else 00316 decode_pitch_lag_high(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap, 00317 &ctx->base_pitch_lag, subframe); 00318 00319 ctx->pitch_lag_int = pitch_lag_int; 00320 pitch_lag_int += pitch_lag_frac > 0; 00321 00322 /* Calculate the pitch vector by interpolating the past excitation at the 00323 pitch lag using a hamming windowed sinc function */ 00324 ff_acelp_interpolatef(exc, exc + 1 - pitch_lag_int, 00325 ac_inter, 4, 00326 pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4), 00327 LP_ORDER, AMRWB_SFR_SIZE + 1); 00328 00329 /* Check which pitch signal path should be used 00330 * 6k60 and 8k85 modes have the ltp flag set to 0 */ 00331 if (amr_subframe->ltp) { 00332 memcpy(ctx->pitch_vector, exc, AMRWB_SFR_SIZE * sizeof(float)); 00333 } else { 00334 for (i = 0; i < AMRWB_SFR_SIZE; i++) 00335 ctx->pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] + 00336 0.18 * exc[i + 1]; 00337 memcpy(exc, ctx->pitch_vector, AMRWB_SFR_SIZE * sizeof(float)); 00338 } 00339 } 00340 00342 #define BIT_STR(x,lsb,len) (((x) >> (lsb)) & ((1 << (len)) - 1)) 00343 00345 #define BIT_POS(x, p) (((x) >> (p)) & 1) 00346 00360 static inline void decode_1p_track(int *out, int code, int m, int off) 00361 { 00362 int pos = BIT_STR(code, 0, m) + off; 00363 00364 out[0] = BIT_POS(code, m) ? -pos : pos; 00365 } 00366 00367 static inline void decode_2p_track(int *out, int code, int m, int off) 00368 { 00369 int pos0 = BIT_STR(code, m, m) + off; 00370 int pos1 = BIT_STR(code, 0, m) + off; 00371 00372 out[0] = BIT_POS(code, 2*m) ? -pos0 : pos0; 00373 out[1] = BIT_POS(code, 2*m) ? -pos1 : pos1; 00374 out[1] = pos0 > pos1 ? -out[1] : out[1]; 00375 } 00376 00377 static void decode_3p_track(int *out, int code, int m, int off) 00378 { 00379 int half_2p = BIT_POS(code, 2*m - 1) << (m - 1); 00380 00381 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1), 00382 m - 1, off + half_2p); 00383 decode_1p_track(out + 2, BIT_STR(code, 2*m, m + 1), m, off); 00384 } 00385 00386 static void decode_4p_track(int *out, int code, int m, int off) 00387 { 00388 int half_4p, subhalf_2p; 00389 int b_offset = 1 << (m - 1); 00390 00391 switch (BIT_STR(code, 4*m - 2, 2)) { /* case ID (2 bits) */ 00392 case 0: /* 0 pulses in A, 4 pulses in B or vice versa */ 00393 half_4p = BIT_POS(code, 4*m - 3) << (m - 1); // which has 4 pulses 00394 subhalf_2p = BIT_POS(code, 2*m - 3) << (m - 2); 00395 00396 decode_2p_track(out, BIT_STR(code, 0, 2*m - 3), 00397 m - 2, off + half_4p + subhalf_2p); 00398 decode_2p_track(out + 2, BIT_STR(code, 2*m - 2, 2*m - 1), 00399 m - 1, off + half_4p); 00400 break; 00401 case 1: /* 1 pulse in A, 3 pulses in B */ 00402 decode_1p_track(out, BIT_STR(code, 3*m - 2, m), 00403 m - 1, off); 00404 decode_3p_track(out + 1, BIT_STR(code, 0, 3*m - 2), 00405 m - 1, off + b_offset); 00406 break; 00407 case 2: /* 2 pulses in each half */ 00408 decode_2p_track(out, BIT_STR(code, 2*m - 1, 2*m - 1), 00409 m - 1, off); 00410 decode_2p_track(out + 2, BIT_STR(code, 0, 2*m - 1), 00411 m - 1, off + b_offset); 00412 break; 00413 case 3: /* 3 pulses in A, 1 pulse in B */ 00414 decode_3p_track(out, BIT_STR(code, m, 3*m - 2), 00415 m - 1, off); 00416 decode_1p_track(out + 3, BIT_STR(code, 0, m), 00417 m - 1, off + b_offset); 00418 break; 00419 } 00420 } 00421 00422 static void decode_5p_track(int *out, int code, int m, int off) 00423 { 00424 int half_3p = BIT_POS(code, 5*m - 1) << (m - 1); 00425 00426 decode_3p_track(out, BIT_STR(code, 2*m + 1, 3*m - 2), 00427 m - 1, off + half_3p); 00428 00429 decode_2p_track(out + 3, BIT_STR(code, 0, 2*m + 1), m, off); 00430 } 00431 00432 static void decode_6p_track(int *out, int code, int m, int off) 00433 { 00434 int b_offset = 1 << (m - 1); 00435 /* which half has more pulses in cases 0 to 2 */ 00436 int half_more = BIT_POS(code, 6*m - 5) << (m - 1); 00437 int half_other = b_offset - half_more; 00438 00439 switch (BIT_STR(code, 6*m - 4, 2)) { /* case ID (2 bits) */ 00440 case 0: /* 0 pulses in A, 6 pulses in B or vice versa */ 00441 decode_1p_track(out, BIT_STR(code, 0, m), 00442 m - 1, off + half_more); 00443 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5), 00444 m - 1, off + half_more); 00445 break; 00446 case 1: /* 1 pulse in A, 5 pulses in B or vice versa */ 00447 decode_1p_track(out, BIT_STR(code, 0, m), 00448 m - 1, off + half_other); 00449 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5), 00450 m - 1, off + half_more); 00451 break; 00452 case 2: /* 2 pulses in A, 4 pulses in B or vice versa */ 00453 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1), 00454 m - 1, off + half_other); 00455 decode_4p_track(out + 2, BIT_STR(code, 2*m - 1, 4*m - 4), 00456 m - 1, off + half_more); 00457 break; 00458 case 3: /* 3 pulses in A, 3 pulses in B */ 00459 decode_3p_track(out, BIT_STR(code, 3*m - 2, 3*m - 2), 00460 m - 1, off); 00461 decode_3p_track(out + 3, BIT_STR(code, 0, 3*m - 2), 00462 m - 1, off + b_offset); 00463 break; 00464 } 00465 } 00466 00476 static void decode_fixed_vector(float *fixed_vector, const uint16_t *pulse_hi, 00477 const uint16_t *pulse_lo, const enum Mode mode) 00478 { 00479 /* sig_pos stores for each track the decoded pulse position indexes 00480 * (1-based) multiplied by its corresponding amplitude (+1 or -1) */ 00481 int sig_pos[4][6]; 00482 int spacing = (mode == MODE_6k60) ? 2 : 4; 00483 int i, j; 00484 00485 switch (mode) { 00486 case MODE_6k60: 00487 for (i = 0; i < 2; i++) 00488 decode_1p_track(sig_pos[i], pulse_lo[i], 5, 1); 00489 break; 00490 case MODE_8k85: 00491 for (i = 0; i < 4; i++) 00492 decode_1p_track(sig_pos[i], pulse_lo[i], 4, 1); 00493 break; 00494 case MODE_12k65: 00495 for (i = 0; i < 4; i++) 00496 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1); 00497 break; 00498 case MODE_14k25: 00499 for (i = 0; i < 2; i++) 00500 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1); 00501 for (i = 2; i < 4; i++) 00502 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1); 00503 break; 00504 case MODE_15k85: 00505 for (i = 0; i < 4; i++) 00506 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1); 00507 break; 00508 case MODE_18k25: 00509 for (i = 0; i < 4; i++) 00510 decode_4p_track(sig_pos[i], (int) pulse_lo[i] + 00511 ((int) pulse_hi[i] << 14), 4, 1); 00512 break; 00513 case MODE_19k85: 00514 for (i = 0; i < 2; i++) 00515 decode_5p_track(sig_pos[i], (int) pulse_lo[i] + 00516 ((int) pulse_hi[i] << 10), 4, 1); 00517 for (i = 2; i < 4; i++) 00518 decode_4p_track(sig_pos[i], (int) pulse_lo[i] + 00519 ((int) pulse_hi[i] << 14), 4, 1); 00520 break; 00521 case MODE_23k05: 00522 case MODE_23k85: 00523 for (i = 0; i < 4; i++) 00524 decode_6p_track(sig_pos[i], (int) pulse_lo[i] + 00525 ((int) pulse_hi[i] << 11), 4, 1); 00526 break; 00527 } 00528 00529 memset(fixed_vector, 0, sizeof(float) * AMRWB_SFR_SIZE); 00530 00531 for (i = 0; i < 4; i++) 00532 for (j = 0; j < pulses_nb_per_mode_tr[mode][i]; j++) { 00533 int pos = (FFABS(sig_pos[i][j]) - 1) * spacing + i; 00534 00535 fixed_vector[pos] += sig_pos[i][j] < 0 ? -1.0 : 1.0; 00536 } 00537 } 00538 00547 static void decode_gains(const uint8_t vq_gain, const enum Mode mode, 00548 float *fixed_gain_factor, float *pitch_gain) 00549 { 00550 const int16_t *gains = (mode <= MODE_8k85 ? qua_gain_6b[vq_gain] : 00551 qua_gain_7b[vq_gain]); 00552 00553 *pitch_gain = gains[0] * (1.0f / (1 << 14)); 00554 *fixed_gain_factor = gains[1] * (1.0f / (1 << 11)); 00555 } 00556 00563 // XXX: Spec states this procedure should be applied when the pitch 00564 // lag is less than 64, but this checking seems absent in reference and AMR-NB 00565 static void pitch_sharpening(AMRWBContext *ctx, float *fixed_vector) 00566 { 00567 int i; 00568 00569 /* Tilt part */ 00570 for (i = AMRWB_SFR_SIZE - 1; i != 0; i--) 00571 fixed_vector[i] -= fixed_vector[i - 1] * ctx->tilt_coef; 00572 00573 /* Periodicity enhancement part */ 00574 for (i = ctx->pitch_lag_int; i < AMRWB_SFR_SIZE; i++) 00575 fixed_vector[i] += fixed_vector[i - ctx->pitch_lag_int] * 0.85; 00576 } 00577 00584 // XXX: There is something wrong with the precision here! The magnitudes 00585 // of the energies are not correct. Please check the reference code carefully 00586 static float voice_factor(float *p_vector, float p_gain, 00587 float *f_vector, float f_gain) 00588 { 00589 double p_ener = (double) ff_dot_productf(p_vector, p_vector, 00590 AMRWB_SFR_SIZE) * p_gain * p_gain; 00591 double f_ener = (double) ff_dot_productf(f_vector, f_vector, 00592 AMRWB_SFR_SIZE) * f_gain * f_gain; 00593 00594 return (p_ener - f_ener) / (p_ener + f_ener); 00595 } 00596 00607 static float *anti_sparseness(AMRWBContext *ctx, 00608 float *fixed_vector, float *buf) 00609 { 00610 int ir_filter_nr; 00611 00612 if (ctx->fr_cur_mode > MODE_8k85) // no filtering in higher modes 00613 return fixed_vector; 00614 00615 if (ctx->pitch_gain[0] < 0.6) { 00616 ir_filter_nr = 0; // strong filtering 00617 } else if (ctx->pitch_gain[0] < 0.9) { 00618 ir_filter_nr = 1; // medium filtering 00619 } else 00620 ir_filter_nr = 2; // no filtering 00621 00622 /* detect 'onset' */ 00623 if (ctx->fixed_gain[0] > 3.0 * ctx->fixed_gain[1]) { 00624 if (ir_filter_nr < 2) 00625 ir_filter_nr++; 00626 } else { 00627 int i, count = 0; 00628 00629 for (i = 0; i < 6; i++) 00630 if (ctx->pitch_gain[i] < 0.6) 00631 count++; 00632 00633 if (count > 2) 00634 ir_filter_nr = 0; 00635 00636 if (ir_filter_nr > ctx->prev_ir_filter_nr + 1) 00637 ir_filter_nr--; 00638 } 00639 00640 /* update ir filter strength history */ 00641 ctx->prev_ir_filter_nr = ir_filter_nr; 00642 00643 ir_filter_nr += (ctx->fr_cur_mode == MODE_8k85); 00644 00645 if (ir_filter_nr < 2) { 00646 int i; 00647 const float *coef = ir_filters_lookup[ir_filter_nr]; 00648 00649 /* Circular convolution code in the reference 00650 * decoder was modified to avoid using one 00651 * extra array. The filtered vector is given by: 00652 * 00653 * c2(n) = sum(i,0,len-1){ c(i) * coef( (n - i + len) % len ) } 00654 */ 00655 00656 memset(buf, 0, sizeof(float) * AMRWB_SFR_SIZE); 00657 for (i = 0; i < AMRWB_SFR_SIZE; i++) 00658 if (fixed_vector[i]) 00659 ff_celp_circ_addf(buf, buf, coef, i, fixed_vector[i], 00660 AMRWB_SFR_SIZE); 00661 fixed_vector = buf; 00662 } 00663 00664 return fixed_vector; 00665 } 00666 00671 static float stability_factor(const float *isf, const float *isf_past) 00672 { 00673 int i; 00674 float acc = 0.0; 00675 00676 for (i = 0; i < LP_ORDER - 1; i++) 00677 acc += (isf[i] - isf_past[i]) * (isf[i] - isf_past[i]); 00678 00679 // XXX: This part is not so clear from the reference code 00680 // the result is more accurate changing the "/ 256" to "* 512" 00681 return FFMAX(0.0, 1.25 - acc * 0.8 * 512); 00682 } 00683 00695 static float noise_enhancer(float fixed_gain, float *prev_tr_gain, 00696 float voice_fac, float stab_fac) 00697 { 00698 float sm_fac = 0.5 * (1 - voice_fac) * stab_fac; 00699 float g0; 00700 00701 // XXX: the following fixed-point constants used to in(de)crement 00702 // gain by 1.5dB were taken from the reference code, maybe it could 00703 // be simpler 00704 if (fixed_gain < *prev_tr_gain) { 00705 g0 = FFMIN(*prev_tr_gain, fixed_gain + fixed_gain * 00706 (6226 * (1.0f / (1 << 15)))); // +1.5 dB 00707 } else 00708 g0 = FFMAX(*prev_tr_gain, fixed_gain * 00709 (27536 * (1.0f / (1 << 15)))); // -1.5 dB 00710 00711 *prev_tr_gain = g0; // update next frame threshold 00712 00713 return sm_fac * g0 + (1 - sm_fac) * fixed_gain; 00714 } 00715 00722 static void pitch_enhancer(float *fixed_vector, float voice_fac) 00723 { 00724 int i; 00725 float cpe = 0.125 * (1 + voice_fac); 00726 float last = fixed_vector[0]; // holds c(i - 1) 00727 00728 fixed_vector[0] -= cpe * fixed_vector[1]; 00729 00730 for (i = 1; i < AMRWB_SFR_SIZE - 1; i++) { 00731 float cur = fixed_vector[i]; 00732 00733 fixed_vector[i] -= cpe * (last + fixed_vector[i + 1]); 00734 last = cur; 00735 } 00736 00737 fixed_vector[AMRWB_SFR_SIZE - 1] -= cpe * last; 00738 } 00739 00750 static void synthesis(AMRWBContext *ctx, float *lpc, float *excitation, 00751 float fixed_gain, const float *fixed_vector, 00752 float *samples) 00753 { 00754 ff_weighted_vector_sumf(excitation, ctx->pitch_vector, fixed_vector, 00755 ctx->pitch_gain[0], fixed_gain, AMRWB_SFR_SIZE); 00756 00757 /* emphasize pitch vector contribution in low bitrate modes */ 00758 if (ctx->pitch_gain[0] > 0.5 && ctx->fr_cur_mode <= MODE_8k85) { 00759 int i; 00760 float energy = ff_dot_productf(excitation, excitation, 00761 AMRWB_SFR_SIZE); 00762 00763 // XXX: Weird part in both ref code and spec. A unknown parameter 00764 // {beta} seems to be identical to the current pitch gain 00765 float pitch_factor = 0.25 * ctx->pitch_gain[0] * ctx->pitch_gain[0]; 00766 00767 for (i = 0; i < AMRWB_SFR_SIZE; i++) 00768 excitation[i] += pitch_factor * ctx->pitch_vector[i]; 00769 00770 ff_scale_vector_to_given_sum_of_squares(excitation, excitation, 00771 energy, AMRWB_SFR_SIZE); 00772 } 00773 00774 ff_celp_lp_synthesis_filterf(samples, lpc, excitation, 00775 AMRWB_SFR_SIZE, LP_ORDER); 00776 } 00777 00787 static void de_emphasis(float *out, float *in, float m, float mem[1]) 00788 { 00789 int i; 00790 00791 out[0] = in[0] + m * mem[0]; 00792 00793 for (i = 1; i < AMRWB_SFR_SIZE; i++) 00794 out[i] = in[i] + out[i - 1] * m; 00795 00796 mem[0] = out[AMRWB_SFR_SIZE - 1]; 00797 } 00798 00807 static void upsample_5_4(float *out, const float *in, int o_size) 00808 { 00809 const float *in0 = in - UPS_FIR_SIZE + 1; 00810 int i, j, k; 00811 int int_part = 0, frac_part; 00812 00813 i = 0; 00814 for (j = 0; j < o_size / 5; j++) { 00815 out[i] = in[int_part]; 00816 frac_part = 4; 00817 i++; 00818 00819 for (k = 1; k < 5; k++) { 00820 out[i] = ff_dot_productf(in0 + int_part, upsample_fir[4 - frac_part], 00821 UPS_MEM_SIZE); 00822 int_part++; 00823 frac_part--; 00824 i++; 00825 } 00826 } 00827 } 00828 00838 static float find_hb_gain(AMRWBContext *ctx, const float *synth, 00839 uint16_t hb_idx, uint8_t vad) 00840 { 00841 int wsp = (vad > 0); 00842 float tilt; 00843 00844 if (ctx->fr_cur_mode == MODE_23k85) 00845 return qua_hb_gain[hb_idx] * (1.0f / (1 << 14)); 00846 00847 tilt = ff_dot_productf(synth, synth + 1, AMRWB_SFR_SIZE - 1) / 00848 ff_dot_productf(synth, synth, AMRWB_SFR_SIZE); 00849 00850 /* return gain bounded by [0.1, 1.0] */ 00851 return av_clipf((1.0 - FFMAX(0.0, tilt)) * (1.25 - 0.25 * wsp), 0.1, 1.0); 00852 } 00853 00863 static void scaled_hb_excitation(AMRWBContext *ctx, float *hb_exc, 00864 const float *synth_exc, float hb_gain) 00865 { 00866 int i; 00867 float energy = ff_dot_productf(synth_exc, synth_exc, AMRWB_SFR_SIZE); 00868 00869 /* Generate a white-noise excitation */ 00870 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++) 00871 hb_exc[i] = 32768.0 - (uint16_t) av_lfg_get(&ctx->prng); 00872 00873 ff_scale_vector_to_given_sum_of_squares(hb_exc, hb_exc, 00874 energy * hb_gain * hb_gain, 00875 AMRWB_SFR_SIZE_16k); 00876 } 00877 00881 static float auto_correlation(float *diff_isf, float mean, int lag) 00882 { 00883 int i; 00884 float sum = 0.0; 00885 00886 for (i = 7; i < LP_ORDER - 2; i++) { 00887 float prod = (diff_isf[i] - mean) * (diff_isf[i - lag] - mean); 00888 sum += prod * prod; 00889 } 00890 return sum; 00891 } 00892 00900 static void extrapolate_isf(float out[LP_ORDER_16k], float isf[LP_ORDER]) 00901 { 00902 float diff_isf[LP_ORDER - 2], diff_mean; 00903 float *diff_hi = diff_isf - LP_ORDER + 1; // diff array for extrapolated indexes 00904 float corr_lag[3]; 00905 float est, scale; 00906 int i, i_max_corr; 00907 00908 memcpy(out, isf, (LP_ORDER - 1) * sizeof(float)); 00909 out[LP_ORDER_16k - 1] = isf[LP_ORDER - 1]; 00910 00911 /* Calculate the difference vector */ 00912 for (i = 0; i < LP_ORDER - 2; i++) 00913 diff_isf[i] = isf[i + 1] - isf[i]; 00914 00915 diff_mean = 0.0; 00916 for (i = 2; i < LP_ORDER - 2; i++) 00917 diff_mean += diff_isf[i] * (1.0f / (LP_ORDER - 4)); 00918 00919 /* Find which is the maximum autocorrelation */ 00920 i_max_corr = 0; 00921 for (i = 0; i < 3; i++) { 00922 corr_lag[i] = auto_correlation(diff_isf, diff_mean, i + 2); 00923 00924 if (corr_lag[i] > corr_lag[i_max_corr]) 00925 i_max_corr = i; 00926 } 00927 i_max_corr++; 00928 00929 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++) 00930 out[i] = isf[i - 1] + isf[i - 1 - i_max_corr] 00931 - isf[i - 2 - i_max_corr]; 00932 00933 /* Calculate an estimate for ISF(18) and scale ISF based on the error */ 00934 est = 7965 + (out[2] - out[3] - out[4]) / 6.0; 00935 scale = 0.5 * (FFMIN(est, 7600) - out[LP_ORDER - 2]) / 00936 (out[LP_ORDER_16k - 2] - out[LP_ORDER - 2]); 00937 00938 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++) 00939 diff_hi[i] = scale * (out[i] - out[i - 1]); 00940 00941 /* Stability insurance */ 00942 for (i = LP_ORDER; i < LP_ORDER_16k - 1; i++) 00943 if (diff_hi[i] + diff_hi[i - 1] < 5.0) { 00944 if (diff_hi[i] > diff_hi[i - 1]) { 00945 diff_hi[i - 1] = 5.0 - diff_hi[i]; 00946 } else 00947 diff_hi[i] = 5.0 - diff_hi[i - 1]; 00948 } 00949 00950 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++) 00951 out[i] = out[i - 1] + diff_hi[i] * (1.0f / (1 << 15)); 00952 00953 /* Scale the ISF vector for 16000 Hz */ 00954 for (i = 0; i < LP_ORDER_16k - 1; i++) 00955 out[i] *= 0.8; 00956 } 00957 00967 static void lpc_weighting(float *out, const float *lpc, float gamma, int size) 00968 { 00969 int i; 00970 float fac = gamma; 00971 00972 for (i = 0; i < size; i++) { 00973 out[i] = lpc[i] * fac; 00974 fac *= gamma; 00975 } 00976 } 00977 00989 static void hb_synthesis(AMRWBContext *ctx, int subframe, float *samples, 00990 const float *exc, const float *isf, const float *isf_past) 00991 { 00992 float hb_lpc[LP_ORDER_16k]; 00993 enum Mode mode = ctx->fr_cur_mode; 00994 00995 if (mode == MODE_6k60) { 00996 float e_isf[LP_ORDER_16k]; // ISF vector for extrapolation 00997 double e_isp[LP_ORDER_16k]; 00998 00999 ff_weighted_vector_sumf(e_isf, isf_past, isf, isfp_inter[subframe], 01000 1.0 - isfp_inter[subframe], LP_ORDER); 01001 01002 extrapolate_isf(e_isf, e_isf); 01003 01004 e_isf[LP_ORDER_16k - 1] *= 2.0; 01005 ff_acelp_lsf2lspd(e_isp, e_isf, LP_ORDER_16k); 01006 ff_amrwb_lsp2lpc(e_isp, hb_lpc, LP_ORDER_16k); 01007 01008 lpc_weighting(hb_lpc, hb_lpc, 0.9, LP_ORDER_16k); 01009 } else { 01010 lpc_weighting(hb_lpc, ctx->lp_coef[subframe], 0.6, LP_ORDER); 01011 } 01012 01013 ff_celp_lp_synthesis_filterf(samples, hb_lpc, exc, AMRWB_SFR_SIZE_16k, 01014 (mode == MODE_6k60) ? LP_ORDER_16k : LP_ORDER); 01015 } 01016 01028 static void hb_fir_filter(float *out, const float fir_coef[HB_FIR_SIZE + 1], 01029 float mem[HB_FIR_SIZE], const float *in) 01030 { 01031 int i, j; 01032 float data[AMRWB_SFR_SIZE_16k + HB_FIR_SIZE]; // past and current samples 01033 01034 memcpy(data, mem, HB_FIR_SIZE * sizeof(float)); 01035 memcpy(data + HB_FIR_SIZE, in, AMRWB_SFR_SIZE_16k * sizeof(float)); 01036 01037 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++) { 01038 out[i] = 0.0; 01039 for (j = 0; j <= HB_FIR_SIZE; j++) 01040 out[i] += data[i + j] * fir_coef[j]; 01041 } 01042 01043 memcpy(mem, data + AMRWB_SFR_SIZE_16k, HB_FIR_SIZE * sizeof(float)); 01044 } 01045 01049 static void update_sub_state(AMRWBContext *ctx) 01050 { 01051 memmove(&ctx->excitation_buf[0], &ctx->excitation_buf[AMRWB_SFR_SIZE], 01052 (AMRWB_P_DELAY_MAX + LP_ORDER + 1) * sizeof(float)); 01053 01054 memmove(&ctx->pitch_gain[1], &ctx->pitch_gain[0], 5 * sizeof(float)); 01055 memmove(&ctx->fixed_gain[1], &ctx->fixed_gain[0], 1 * sizeof(float)); 01056 01057 memmove(&ctx->samples_az[0], &ctx->samples_az[AMRWB_SFR_SIZE], 01058 LP_ORDER * sizeof(float)); 01059 memmove(&ctx->samples_up[0], &ctx->samples_up[AMRWB_SFR_SIZE], 01060 UPS_MEM_SIZE * sizeof(float)); 01061 memmove(&ctx->samples_hb[0], &ctx->samples_hb[AMRWB_SFR_SIZE_16k], 01062 LP_ORDER_16k * sizeof(float)); 01063 } 01064 01065 static int amrwb_decode_frame(AVCodecContext *avctx, void *data, int *data_size, 01066 AVPacket *avpkt) 01067 { 01068 AMRWBContext *ctx = avctx->priv_data; 01069 AMRWBFrame *cf = &ctx->frame; 01070 const uint8_t *buf = avpkt->data; 01071 int buf_size = avpkt->size; 01072 int expected_fr_size, header_size; 01073 float *buf_out = data; 01074 float spare_vector[AMRWB_SFR_SIZE]; // extra stack space to hold result from anti-sparseness processing 01075 float fixed_gain_factor; // fixed gain correction factor (gamma) 01076 float *synth_fixed_vector; // pointer to the fixed vector that synthesis should use 01077 float synth_fixed_gain; // the fixed gain that synthesis should use 01078 float voice_fac, stab_fac; // parameters used for gain smoothing 01079 float synth_exc[AMRWB_SFR_SIZE]; // post-processed excitation for synthesis 01080 float hb_exc[AMRWB_SFR_SIZE_16k]; // excitation for the high frequency band 01081 float hb_samples[AMRWB_SFR_SIZE_16k]; // filtered high-band samples from synthesis 01082 float hb_gain; 01083 int sub, i; 01084 01085 header_size = decode_mime_header(ctx, buf); 01086 expected_fr_size = ((cf_sizes_wb[ctx->fr_cur_mode] + 7) >> 3) + 1; 01087 01088 if (buf_size < expected_fr_size) { 01089 av_log(avctx, AV_LOG_ERROR, 01090 "Frame too small (%d bytes). Truncated file?\n", buf_size); 01091 *data_size = 0; 01092 return buf_size; 01093 } 01094 01095 if (!ctx->fr_quality || ctx->fr_cur_mode > MODE_SID) 01096 av_log(avctx, AV_LOG_ERROR, "Encountered a bad or corrupted frame\n"); 01097 01098 if (ctx->fr_cur_mode == MODE_SID) /* Comfort noise frame */ 01099 av_log_missing_feature(avctx, "SID mode", 1); 01100 01101 if (ctx->fr_cur_mode >= MODE_SID) 01102 return -1; 01103 01104 ff_amr_bit_reorder((uint16_t *) &ctx->frame, sizeof(AMRWBFrame), 01105 buf + header_size, amr_bit_orderings_by_mode[ctx->fr_cur_mode]); 01106 01107 /* Decode the quantized ISF vector */ 01108 if (ctx->fr_cur_mode == MODE_6k60) { 01109 decode_isf_indices_36b(cf->isp_id, ctx->isf_cur); 01110 } else { 01111 decode_isf_indices_46b(cf->isp_id, ctx->isf_cur); 01112 } 01113 01114 isf_add_mean_and_past(ctx->isf_cur, ctx->isf_q_past); 01115 ff_set_min_dist_lsf(ctx->isf_cur, MIN_ISF_SPACING, LP_ORDER - 1); 01116 01117 stab_fac = stability_factor(ctx->isf_cur, ctx->isf_past_final); 01118 01119 ctx->isf_cur[LP_ORDER - 1] *= 2.0; 01120 ff_acelp_lsf2lspd(ctx->isp[3], ctx->isf_cur, LP_ORDER); 01121 01122 /* Generate a ISP vector for each subframe */ 01123 if (ctx->first_frame) { 01124 ctx->first_frame = 0; 01125 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(double)); 01126 } 01127 interpolate_isp(ctx->isp, ctx->isp_sub4_past); 01128 01129 for (sub = 0; sub < 4; sub++) 01130 ff_amrwb_lsp2lpc(ctx->isp[sub], ctx->lp_coef[sub], LP_ORDER); 01131 01132 for (sub = 0; sub < 4; sub++) { 01133 const AMRWBSubFrame *cur_subframe = &cf->subframe[sub]; 01134 float *sub_buf = buf_out + sub * AMRWB_SFR_SIZE_16k; 01135 01136 /* Decode adaptive codebook (pitch vector) */ 01137 decode_pitch_vector(ctx, cur_subframe, sub); 01138 /* Decode innovative codebook (fixed vector) */ 01139 decode_fixed_vector(ctx->fixed_vector, cur_subframe->pul_ih, 01140 cur_subframe->pul_il, ctx->fr_cur_mode); 01141 01142 pitch_sharpening(ctx, ctx->fixed_vector); 01143 01144 decode_gains(cur_subframe->vq_gain, ctx->fr_cur_mode, 01145 &fixed_gain_factor, &ctx->pitch_gain[0]); 01146 01147 ctx->fixed_gain[0] = 01148 ff_amr_set_fixed_gain(fixed_gain_factor, 01149 ff_dot_productf(ctx->fixed_vector, ctx->fixed_vector, 01150 AMRWB_SFR_SIZE) / AMRWB_SFR_SIZE, 01151 ctx->prediction_error, 01152 ENERGY_MEAN, energy_pred_fac); 01153 01154 /* Calculate voice factor and store tilt for next subframe */ 01155 voice_fac = voice_factor(ctx->pitch_vector, ctx->pitch_gain[0], 01156 ctx->fixed_vector, ctx->fixed_gain[0]); 01157 ctx->tilt_coef = voice_fac * 0.25 + 0.25; 01158 01159 /* Construct current excitation */ 01160 for (i = 0; i < AMRWB_SFR_SIZE; i++) { 01161 ctx->excitation[i] *= ctx->pitch_gain[0]; 01162 ctx->excitation[i] += ctx->fixed_gain[0] * ctx->fixed_vector[i]; 01163 ctx->excitation[i] = truncf(ctx->excitation[i]); 01164 } 01165 01166 /* Post-processing of excitation elements */ 01167 synth_fixed_gain = noise_enhancer(ctx->fixed_gain[0], &ctx->prev_tr_gain, 01168 voice_fac, stab_fac); 01169 01170 synth_fixed_vector = anti_sparseness(ctx, ctx->fixed_vector, 01171 spare_vector); 01172 01173 pitch_enhancer(synth_fixed_vector, voice_fac); 01174 01175 synthesis(ctx, ctx->lp_coef[sub], synth_exc, synth_fixed_gain, 01176 synth_fixed_vector, &ctx->samples_az[LP_ORDER]); 01177 01178 /* Synthesis speech post-processing */ 01179 de_emphasis(&ctx->samples_up[UPS_MEM_SIZE], 01180 &ctx->samples_az[LP_ORDER], PREEMPH_FAC, ctx->demph_mem); 01181 01182 ff_acelp_apply_order_2_transfer_function(&ctx->samples_up[UPS_MEM_SIZE], 01183 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_31_poles, 01184 hpf_31_gain, ctx->hpf_31_mem, AMRWB_SFR_SIZE); 01185 01186 upsample_5_4(sub_buf, &ctx->samples_up[UPS_FIR_SIZE], 01187 AMRWB_SFR_SIZE_16k); 01188 01189 /* High frequency band (6.4 - 7.0 kHz) generation part */ 01190 ff_acelp_apply_order_2_transfer_function(hb_samples, 01191 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_400_poles, 01192 hpf_400_gain, ctx->hpf_400_mem, AMRWB_SFR_SIZE); 01193 01194 hb_gain = find_hb_gain(ctx, hb_samples, 01195 cur_subframe->hb_gain, cf->vad); 01196 01197 scaled_hb_excitation(ctx, hb_exc, synth_exc, hb_gain); 01198 01199 hb_synthesis(ctx, sub, &ctx->samples_hb[LP_ORDER_16k], 01200 hb_exc, ctx->isf_cur, ctx->isf_past_final); 01201 01202 /* High-band post-processing filters */ 01203 hb_fir_filter(hb_samples, bpf_6_7_coef, ctx->bpf_6_7_mem, 01204 &ctx->samples_hb[LP_ORDER_16k]); 01205 01206 if (ctx->fr_cur_mode == MODE_23k85) 01207 hb_fir_filter(hb_samples, lpf_7_coef, ctx->lpf_7_mem, 01208 hb_samples); 01209 01210 /* Add the low and high frequency bands */ 01211 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++) 01212 sub_buf[i] = (sub_buf[i] + hb_samples[i]) * (1.0f / (1 << 15)); 01213 01214 /* Update buffers and history */ 01215 update_sub_state(ctx); 01216 } 01217 01218 /* update state for next frame */ 01219 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(ctx->isp[3][0])); 01220 memcpy(ctx->isf_past_final, ctx->isf_cur, LP_ORDER * sizeof(float)); 01221 01222 /* report how many samples we got */ 01223 *data_size = 4 * AMRWB_SFR_SIZE_16k * sizeof(float); 01224 01225 return expected_fr_size; 01226 } 01227 01228 AVCodec ff_amrwb_decoder = { 01229 .name = "amrwb", 01230 .type = AVMEDIA_TYPE_AUDIO, 01231 .id = CODEC_ID_AMR_WB, 01232 .priv_data_size = sizeof(AMRWBContext), 01233 .init = amrwb_decode_init, 01234 .decode = amrwb_decode_frame, 01235 .long_name = NULL_IF_CONFIG_SMALL("Adaptive Multi-Rate WideBand"), 01236 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE}, 01237 };